I'm getting a when trying to incorporate embedding into my GAN error:
AssertionError: Could not compute output KerasTensor(type_spec=TensorSpec(shape=(None, 1), dtype=tf.float32, name=None), name='dense_270/BiasAdd:0', description="created by layer 'dense_270'")
I was playing with this code by sending it a straight numpy array. This is the working version without embeddings:
# Build Generative model ...
g_input = Input(shape=[Xtrain.shape[1]])
H1 = Dense(512)(g_input)
H1r = Activation('relu')(H1)
H2 = Dense(256)(H1r)
H2r = Activation('relu')(H2)
H3 = Dense(256)(H2r)
H3r = Activation('relu')(H3)
H4 = Dense(128)(H3r)
H4r = Activation('relu')(H4)
H5 = Dense(128)(H4r)
H5r = Activation('relu')(H5)
H6 = Dense(64)(H5r)
H6r = Activation('relu')(H6)
H7 = Dense(32)(H6r)
Hr = Activation('relu')(H7)
g_V = Dense(1)(Hr)
generator = Model(g_input,g_V)
generator.compile(loss='binary_crossentropy', optimizer=opt)
generator.summary()
# Build Discriminative model ...
d_input = Input(shape=[Xtrain.shape[1]+1])
H = Dense(256)(d_input)
H = Activation('relu')(H)
H = Dense(256)(H)
H = Activation('relu')(H)
H = Dense(64)(H)
H = Activation('relu')(H)
H = Dense(32)(H)
H = Activation('relu')(H)
d_V = Dense(2,activation='softmax')(H)
discriminator = Model(d_input,d_V)
discriminator.compile(loss='categorical_crossentropy', optimizer=dopt)
discriminator.summary()
# Freeze weights in the discriminator for stacked training
make_trainable(discriminator, False)
# Build stacked GAN model
gan_input = Input(shape=Xtrain.shape[1])
H = generator(gan_input)
gd_input=Concatenate()([gan_input,H])
gan_V = discriminator(gd_input)
GAN = Model(gan_input, [gan_V,H])
GAN.compile(loss=['categorical_crossentropy','mse'], optimizer=opt) #Complete GAN have both loss functions
GAN.summary()
Then, I took my original numpy array and added 2 numpy arrays into each row so it's basically a few values then an array. Like this -
[32,53, .43, [1,2,3,4],[5,6,7,8]]
To use this model with that modifications in the data, I made the following changes:
# Build Generative model ...
g_input = Input(shape=[len(total_x[0])-2]) #have a array that's only a numpy array by excluding last 2 items)
np_input = Input(shape=[len(total_x[0][:-1])]) #include only the last arrray
mergedLayer = layers.concatenate([g_input, np_input]) #merge both
H1 = Dense(512)(mergedLayer) #everything after is the same
Then I changed the generator to accept two inputs instead of one:
generator = Model([g_input,np_input],g_V)
But then I get the above error on this part:
H = generator(gan_input)
I'm new to ML but I thought based on the above, maybe I can pass it a concatenated layer but it didn't work(basically the same logic as above):
gan_dataframe_input = Input(shape=[Xtrain.shape[1]-2])
numpy_input = Input(shape=2)
gan_input = layers.concatenate([gan_dataframe_input, numpy_input])
H = generator(gan_input)
I hope this is enough info to help or give me suggestions. Here's the full stack trace if it helps:
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-92-52760dc68480> in <module>()
54 gan_input = layers.concatenate([gan_dataframe_input, numpy_input])
55
---> 56 H = generator(gan_dataframe_input)
57 gd_input=Concatenate()([gan_input,H])
58 gan_V = discriminator(gd_input)
5 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
950 if _in_functional_construction_mode(self, inputs, args, kwargs, input_list):
951 return self._functional_construction_call(inputs, args, kwargs,
--> 952 input_list)
953
954 # Maintains info about the `Layer.call` stack.
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in _functional_construction_call(self, inputs, args, kwargs, input_list)
1089 # Check input assumptions set after layer building, e.g. input shape.
1090 outputs = self._keras_tensor_symbolic_call(
-> 1091 inputs, input_masks, args, kwargs)
1092
1093 if outputs is None:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in _keras_tensor_symbolic_call(self, inputs, input_masks, args, kwargs)
820 return nest.map_structure(keras_tensor.KerasTensor, output_signature)
821 else:
--> 822 return self._infer_output_signature(inputs, args, kwargs, input_masks)
823
824 def _infer_output_signature(self, inputs, args, kwargs, input_masks):
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in _infer_output_signature(self, inputs, args, kwargs, input_masks)
861 # TODO(kaftan): do we maybe_build here, or have we already done it?
862 self._maybe_build(inputs)
--> 863 outputs = call_fn(inputs, *args, **kwargs)
864
865 self._handle_activity_regularization(inputs, outputs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/functional.py in call(self, inputs, training, mask)
423 """
424 return self._run_internal_graph(
--> 425 inputs, training=training, mask=mask)
426
427 def compute_output_shape(self, input_shape):
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/functional.py in _run_internal_graph(self, inputs, training, mask)
567 for x in self.outputs:
568 x_id = str(id(x))
--> 569 assert x_id in tensor_dict, 'Could not compute output ' + str(x)
570 output_tensors.append(tensor_dict[x_id].pop())
571
AssertionError: Could not compute output KerasTensor(type_spec=TensorSpec(shape=(None, 1), dtype=tf.float32, name=None), name='dense_270/BiasAdd:0', description="created by layer 'dense_270'")
Also if it helps, here's the code above with the modifications I made that's generating this error:
# Build Generative model ...
g_input = Input(shape=[len(total_x[0])-2])
np_input = Input(shape=[len(total_x[0][:-1])])
mergedLayer = layers.concatenate([g_input, np_input])
H1 = Dense(512)(mergedLayer)
H1r = Activation('relu')(H1)
H2 = Dense(256)(H1r)
H2r = Activation('relu')(H2)
H3 = Dense(256)(H2r)
H3r = Activation('relu')(H3)
H4 = Dense(128)(H3r)
H4r = Activation('relu')(H4)
H5 = Dense(128)(H4r)
H5r = Activation('relu')(H5)
H6 = Dense(64)(H5r)
H6r = Activation('relu')(H6)
H7 = Dense(32)(H6r)
Hr = Activation('relu')(H7)
g_V = Dense(1)(Hr)
generator = Model([g_input,np_input],g_V)
generator.compile(loss='binary_crossentropy', optimizer=opt)
# generator.summary()
# utils.plot_model(generator, show_layer_names=False, show_shapes=True)
# Build Discriminative model ...
d_input = Input(shape=[Xtrain.shape[1]+1])
H = Dense(256)(d_input)
H = Activation('relu')(H)
H = Dense(256)(H)
H = Activation('relu')(H)
H = Dense(64)(H)
H = Activation('relu')(H)
H = Dense(32)(H)
H = Activation('relu')(H)
d_V = Dense(2,activation='softmax')(H)
discriminator = Model(d_input,d_V)
discriminator.compile(loss='categorical_crossentropy', optimizer=dopt)
# discriminator.summary()
# Freeze weights in the discriminator for stacked training
make_trainable(discriminator, False)
# Build stacked GAN model
# gan_input = Input(shape=Xtrain.shape[1]) #old
gan_dataframe_input = Input(shape=[Xtrain.shape[1]-2])
numpy_input = Input(shape=2)
gan_input = layers.concatenate([gan_dataframe_input, numpy_input])
H = generator(gan_input)
gd_input=Concatenate()([gan_input,H])
gan_V = discriminator(gd_input)
GAN = Model(gan_input, [gan_V,H])
GAN.compile(loss=['categorical_crossentropy','mse'], optimizer=opt) #Complete GAN have both loss functions
GAN.summary()
Any suggestions or resources to look at would be helpful!
UPDATE - I'm not sure if I'm getting closer or further. I made the following changes(to try to get the shape to match):
gan_dataframe_input = Input(shape=[len(total_x[0])-2])
numpy_input = Input(shape=[len(total_x[0][:-1])])
gan_input = layers.concatenate([gan_dataframe_input, numpy_input])
H = generator([gan_dataframe_input,numpy_input])
gd_input=Concatenate()([gan_input,H])
gan_V = discriminator(gd_input)
GAN = Model(gan_input, [gan_V,H])
but now I get a error on the last line:
ValueError: Graph disconnected: cannot obtain value for tensor KerasTensor(type_spec=TensorSpec(shape=(None, 659), dtype=tf.float32, name='input_230'), name='input_230', description="created by layer 'input_230'") at layer "concatenate_94". The following previous layers were accessed without issue: []
I'm not sure if I'm getting closer or not, but the weird thing is my xtrain numpy array is (661,) and my numpy array is (77,0) so I'm not sure why when I print(mergedLayer) I get KerasTensor(type_spec=TensorSpec(shape=(None, 1319), dtype=tf.float32, name=None), name='concatenate_93/concat:0', description="created by layer 'concatenate_93'") It seems it doesn't see the columsn and only the rows? I'm not sure if any of this makes sense, I might be confusing myself more trying to understand multi imputs.