I am running into an issue running a CLDNN model in Python 3. I ran model.summary() and I did not see an issue. I am receiving a sizing/shaping error between Keras and Tensorflow.
Below is the code with the output error. Any input would be greatly apricated.
Update: reformatted the code and supplied the output as text.
def cldnn():
model = Sequential()
#input layer
model.add(keras.layers.InputLayer(input_shape=(2, 128, 1)))
#first layer
model.add(Conv2D(256, (1, 3), activation='relu', use_bias=True))
model.add(Dropout(0.2))
#second layer
model.add(Conv2D(256, (2, 3), activation='relu', use_bias=True))
#third layer
model.add(Conv2D(80, (1, 3), activation='relu', use_bias=True))
model.add(Dropout(0.2))
#fourth layer
model.add(Conv2D(80, (1, 3), activation='relu', use_bias=True))
#reshape data
model.add(Reshape((120,80)))
#fifth layer
model.add(LSTM(50, activation='tanh', return_sequences=True))
#sixth layer
model.add(Dense(128,
activation='relu',
use_bias=True,
kernel_initializer='normal'))
#output layer
model.add(Dense(10, activation='softmax', use_bias=True))
model.summary()
return model
#Create instance of CNN model graph
cldnn = cldnn()
#Compile model using an appropriate loss and optimizer algorithm
cldnn.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
cldnn_mdata = cldnn.fit(data_train, labels_train,
validation_data=(data_test, labels_test),
epochs=15,
batch_size=1024,
shuffle=True)
#Print accuracy of model on testing set after training
scores = cldnn.evaluate(data_test, labels_test)
print("Accuracy: %.2f%%" %(scores[1]*100))
#OUTPUT
Model: "sequential_4"
_________________________________________________________________
Layer (type) Output Shape Param # =================================================================
conv2d_16 (Conv2D) (None, 2, 126, 256) 1024
_________________________________________________________________
dropout_8 (Dropout) (None, 2, 126, 256) 0
_________________________________________________________________
conv2d_17 (Conv2D) (None, 1, 124, 256) 393472
_________________________________________________________________
conv2d_18 (Conv2D) (None, 1, 122, 80) 61520
_________________________________________________________________
dropout_9 (Dropout) (None, 1, 122, 80) 0
_________________________________________________________________
conv2d_19 (Conv2D) (None, 1, 120, 80) 19280
_________________________________________________________________
reshape_3 (Reshape) (None, 120, 80) 0
_________________________________________________________________
lstm_3 (LSTM) (None, 120, 50) 26200
_________________________________________________________________
dense_6 (Dense) (None, 120, 128) 6528
_________________________________________________________________
dense_7 (Dense) (None, 120, 10) 1290
=================================================================
Total params: 509,314
Trainable params: 509,314
Non-trainable params: 0
_________________________________________________________________
Epoch 1/15
WARNING:tensorflow:Model was constructed with shape (None, 2, 128, 1) for input KerasTensor(type_spec=TensorSpec(shape=(None, 2, 128, 1), dtype=tf.float32, name='input_5'), name='input_5', description="created by layer 'input_5'"), but it was called on an input with incompatible shape (None, 28, 28, 1).
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-9-0e0ee3a37c65> in <module>()
52 epochs=15,
53 batch_size=1024,
---> 54 shuffle=True)
55
56 #Print accuracy of model on testing set after training
9 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
992 except Exception as e: # pylint:disable=broad-except
993 if hasattr(e, "ag_error_metadata"):
--> 994 raise e.ag_error_metadata.to_exception(e)
995 else:
996 raise
ValueError: in user code:
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:853 train_function *
return step_function(self, iterator)
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:842 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:1286 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:2849 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:3632 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:835 run_step **
outputs = model.train_step(data)
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:787 train_step
y_pred = self(x, training=True)
/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py:1037 __call__
outputs = call_fn(inputs, *args, **kwargs)
/usr/local/lib/python3.7/dist-packages/keras/engine/sequential.py:369 call
return super(Sequential, self).call(inputs, training=training, mask=mask)
/usr/local/lib/python3.7/dist-packages/keras/engine/functional.py:415 call
inputs, training=training, mask=mask)
/usr/local/lib/python3.7/dist-packages/keras/engine/functional.py:550 _run_internal_graph
outputs = node.layer(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py:1037 __call__
outputs = call_fn(inputs, *args, **kwargs)
/usr/local/lib/python3.7/dist-packages/keras/layers/core.py:539 call
result.set_shape(self.compute_output_shape(inputs.shape))
/usr/local/lib/python3.7/dist-packages/keras/layers/core.py:530 compute_output_shape
self.target_shape)
/usr/local/lib/python3.7/dist-packages/keras/layers/core.py:518 _fix_unknown_dimension
raise ValueError(msg)
ValueError: total size of new array must be unchanged, input_shape = [27, 20, 80], output_shape = [120, 80]
Related
I'm trying to train the model but I am stuck with this error
ValueError: Shapes (None, 1) and (None, 24) are incompatible
My code
model = models.Sequential()
model.add(layers.Conv2D(32, kernel_size=(3, 3), activation='relu',input_shape=(28, 28, 1)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D(pool_size=(2, 2)))
model.add(layers.Dropout(0.25))
model.add(layers.Flatten())
model.add(layers.Dense(128, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(25, activation=tf.nn.softmax))
model.summary()
Model Summary
Model: "sequential_6"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_18 (Conv2D) (None, 26, 26, 32) 320
conv2d_19 (Conv2D) (None, 24, 24, 64) 18496
conv2d_20 (Conv2D) (None, 22, 22, 128) 73856
max_pooling2d_8 (MaxPooling (None, 11, 11, 128) 0
2D)
dropout_8 (Dropout) (None, 11, 11, 128) 0
flatten_5 (Flatten) (None, 15488) 0
dense_10 (Dense) (None, 128) 1982592
dropout_9 (Dropout) (None, 128) 0
dense_11 (Dense) (None, 25) 3225
=================================================================
Total params: 2,078,489
Trainable params: 2,078,489
Non-trainable params: 0
Complier
model.compile(loss=tf.keras.losses.categorical_crossentropy,
optimizer=tf.keras.optimizers.Adadelta(), metrics=['accuracy'])
Fit the model
model.fit(train_img, train_y_values, batch_size=64, epochs=5,
verbose=1, validation_data=(test_img, test_y_values))
Error
Epoch 1/5
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-61-1ace0d4f685d> in <module>
1 model.fit(train_img, train_y_values, batch_size=64, epochs=5,
----> 2 verbose=1, validation_data=(test_img, test_y_values))
1 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py in tf__train_function(iterator)
13 try:
14 do_return = True
---> 15 retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
16 except:
17 do_return = False
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1051, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1040, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1030, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 890, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 949, in compute_loss
y, y_pred, sample_weight, regularization_losses=self.losses)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py", line 201, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 139, in __call__
losses = call_fn(y_true, y_pred)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 243, in call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 1788, in categorical_crossentropy
y_true, y_pred, from_logits=from_logits, axis=axis)
File "/usr/local/lib/python3.7/dist-packages/keras/backend.py", line 5119, in categorical_crossentropy
target.shape.assert_is_compatible_with(output.shape)
ValueError: Shapes (None, 1) and (None, 24) are incompatible
train_img shape = (27455, 28, 28)
train_y_value shape = (27455,)
test_img shape = (7172, 28, 28)
test_y_values shape = (7172,)
Somebody help me please
The problem comes from a mismatch of the last layer of your model (None, 25) and the labels (None, 1).
This can be fixed by changing the loss function to "sparse_categorical_crossentropy"
See What is the difference between sparse_categorical_crossentropy and categorical_crossentropy? for more details.
The code I'm trying to run to create a Neural Network model to predict some damping ratio and added mass coefficient. I have 3 inputs of the model (float numbers) and 4 outputs of the model (float numbers).
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.optimizers import SGD
from keras.models import Model
from keras.layers import Input
from keras.layers import Dense
from keras.layers import Concatenate
from sklearn.model_selection import train_test_split
from Reasearch_project_Code import *
"""Create model"""
Input1 = Input(shape=(1),name="Input1")
Input2 = Input(shape=(1),name="Input2")
Input3 = Input(shape=(1),name="Input3")
Inputs = Concatenate(axis=-1,name="Inputs")([Input1,Input2,Input3])
hidden1 = Dense(100, activation='relu',name="Hidden1")(Inputs)
hidden2 = Dense(100, activation='relu',name="Hidden2")(hidden1)
# Output layer
output1 = Dense(1, activation='linear',name="Output_1")(hidden2)
output2 = Dense(1, activation='linear',name="Output_2")(hidden2)
output3 = Dense(1, activation='linear',name="Output_3")(hidden2)
output4 = Dense(1, activation='linear',name="Output_4")(hidden2)
# output
model = Model(inputs=[Input1,Input2,Input3], outputs=[output1, output2, output3, output4])
# summarize layers
print(model.summary())
opt = SGD(0.001)
model.compile(loss='mean_squared_error', optimizer=opt)
"""Load Data"""
dpath="C:\\Users\\jules\\OneDrive - University of Southampton\\Documents\\Research Project\\5- Codes"
R=RP(dpath)
data="Data/Test3/Test3.csv"
labelss=np.genfromtxt(data,skip_header=1,usecols=(1,2,3),delimiter=",")
sampless=np.genfromtxt(data,skip_header=1,usecols=(4,5,6,7),delimiter=",")
"""scaled data"""
scaler=MinMaxScaler(feature_range=(0,1))
samples = scaler.fit_transform(sampless)
labels = scaler.fit_transform(labelss)
"""split data"""
labels_train, labels_test, samples_train , samples_test= train_test_split(labels,samples,train_size=0.9,random_state=42)
print(f"labels_train:{labels_train.shape}")
print(f"labels_test:{labels_test.shape}")
print(f"samples_train:{samples_train.shape}")
print(f"samples_test:{samples_test.shape}")
history = model.fit(labels_train, samples_train, validation_data=(labels_test, samples_test), epochs=200,batch_size=20, verbose=1)
shape of the data:
labels_train:(6753, 3)
labels_test:(751, 3)
samples_train:(6753, 4)
samples_test:(751, 4)
model:
Model: "functional_115"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
Input1 (InputLayer) [(None, 1)] 0
__________________________________________________________________________________________________
Input2 (InputLayer) [(None, 1)] 0
__________________________________________________________________________________________________
Input3 (InputLayer) [(None, 1)] 0
__________________________________________________________________________________________________
Inputs (Concatenate) (None, 3) 0 Input1[0][0]
Input2[0][0]
Input3[0][0]
__________________________________________________________________________________________________
Hidden1 (Dense) (None, 100) 400 Inputs[0][0]
__________________________________________________________________________________________________
Hidden2 (Dense) (None, 100) 10100 Hidden1[0][0]
__________________________________________________________________________________________________
Output_1 (Dense) (None, 1) 101 Hidden2[0][0]
__________________________________________________________________________________________________
Output_2 (Dense) (None, 1) 101 Hidden2[0][0]
__________________________________________________________________________________________________
Output_3 (Dense) (None, 1) 101 Hidden2[0][0]
__________________________________________________________________________________________________
Output_4 (Dense) (None, 1) 101 Hidden2[0][0]
==================================================================================================
Total params: 10,904
Trainable params: 10,904
Non-trainable params: 0
__________________________________________________________________________________________________
Assertions error:
AssertionError: in user code:
C:\Users\jules\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py:806 train_function *
return step_function(self, iterator)
C:\Users\jules\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py:796 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
C:\Users\jules\anaconda3\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1211 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
C:\Users\jules\anaconda3\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2585 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
C:\Users\jules\anaconda3\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2945 _call_for_each_replica
return fn(*args, **kwargs)
C:\Users\jules\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py:789 run_step **
outputs = model.train_step(data)
C:\Users\jules\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py:747 train_step
y_pred = self(x, training=True)
C:\Users\jules\anaconda3\lib\site-packages\tensorflow\python\keras\engine\base_layer.py:985 __call__
outputs = call_fn(inputs, *args, **kwargs)
C:\Users\jules\anaconda3\lib\site-packages\tensorflow\python\keras\engine\functional.py:385 call
return self._run_internal_graph(
C:\Users\jules\anaconda3\lib\site-packages\tensorflow\python\keras\engine\functional.py:517 _run_internal_graph
assert x_id in tensor_dict, 'Could not compute output ' + str(x)
AssertionError: Could not compute output Tensor("Output_1/BiasAdd_57:0", shape=(None, 1), dtype=float32)
I'm a beginner and I don't understand where the error come from because my inputs and outputs seems to hae the correct dimensions.
Previously, instead of using 3 inputs layers, I used one with a shape of 3:
Inputs = Input(shape=(3,),name="Inputs")
But this gave me terrible predictions (negative R2)
I have been debugging this error for the past week and I'm not sure why my code is not working.
We have a custom environment and our reinforcement learning problem is taking a 512x512 image and deciding if we should do action 1 or action 2.
env = customEnv()
nb_actions = env.action_space.n # 2 options
shape = env.observation_space.shape
pool_size = 2
input_shape = (512, 512, 1) # 1 channel, grayscale image
model = Sequential()
model.add(Convolution2D(32, 3, padding="same", input_shape=input_shape))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(pool_size, pool_size)))
model.add(Convolution2D(64, 2, padding="same"))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(pool_size, pool_size)))
model.add(Convolution2D(64, 2, padding="same"))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(pool_size, pool_size)))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dense(nb_actions))
model.add(Activation('linear'))
print(model.summary())
memory = SequentialMemory(limit=1000000, window_length=WINDOW_LENGTH)
policy = BoltzmannQPolicy()
dqn = DQNAgent(model=model, nb_actions=nb_actions, policy=policy, memory=memory,
nb_steps_warmup=50000, gamma=.99, target_model_update=10000,
train_interval=4, delta_clip=1.)
dqn.compile(Adam(learning_rate=.00025), metrics=['mae'])
dqn.fit(env, nb_steps=50000, visualize=False, verbose=2)
dqn.save_weights(f'dqn_CTEnv_weights.h5f', overwrite=True)
dqn.test(env, nb_episodes=5, visualize=False)
Full error log:
Traceback (most recent call last):
File "DQN_CT.py", line 60, in <module>
dqn.fit(env, nb_steps=50000, visualize=False, verbose=2)
File "/home/anaconda3/envs/lib/python3.7/site-packages/rl/core.py", line 168, in fit
action = self.forward(observation)
File "/home/anaconda3/envs/lib/python3.7/site-packages/rl/agents/dqn.py", line 224, in forward
q_values = self.compute_q_values(state)
File "/home/anaconda3/envs/lib/python3.7/site-packages/rl/agents/dqn.py", line 68, in compute_q_values
q_values = self.compute_batch_q_values([state]).flatten()
File "/home/anaconda3/envs/lib/python3.7/site-packages/rl/agents/dqn.py", line 63, in compute_batch_q_values
q_values = self.model.predict_on_batch(batch)
File "/home/anaconda3/envs/python3.7/site-packages/tensorflow/python/keras/engine/training_v1.py", line 1201, in predict_on_batch
x, extract_tensors_from_dataset=True)
File "/home/anaconda3/envs/lib/python3.7/site-packages/tensorflow/python/keras/engine/training_v1.py", line 2334, in _standardize_user_data
batch_size=batch_size)
File "/home/anaconda3/envs/lib/python3.7/site-packages/tensorflow/python/keras/engine/training_v1.py", line 2361, in _standardize_tensors
exception_prefix='input')
File "/home/anaconda3/envs/lib/python3.7/site-packages/tensorflow/python/keras/engine/training_utils.py", line 574, in standardize_input_data
'with shape ' + str(data_shape))
ValueError: Error when checking input: expected permute_input to have 4 dimensions, but got array with shape (1, 4)
I have looked at a lot of other posts regarding this error and most seems to point out that the input shape needs to be 3D (width, height, channel) which doesn't seem to be working for us. We also tried (batch size (window_length), width, height, channel), but doing so gives us another error ValueError: Input 0 of layer permute is incompatible with the layer: expected ndim=4, found ndim=5. Full shape received: [None, 4, 512, 512, 1]
Any help on this problem would really be appreciated!
Tried your model architecture with different dataset with little modifications .
Working sample code
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential
import pathlib
dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url, untar=True)
data_dir = pathlib.Path(data_dir)
batch_size = 32
img_height = 512
img_width = 512
train_ds = tf.keras.utils.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="training",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
val_ds = tf.keras.utils.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="validation",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
for image_batch, labels_batch in train_ds:
print(image_batch.shape)
print(labels_batch.shape)
break
input_shape = (180, 180, 3) # 3 channel, RGB image
model = Sequential()
model.add(tf.keras.layers.Conv2D(32, 3, activation= 'relu',padding="same", input_shape=input_shape))
#model.add(Activation("relu"))
model.add(tf.keras.layers.MaxPooling2D())
model.add(tf.keras.layers.Conv2D(64, 2, activation= 'relu',padding="same"))
model.add(tf.keras.layers.MaxPooling2D())
model.add(tf.keras.layers.Conv2D(64, 2, activation= 'relu',padding="same"))
#model.add(Activation("relu"))
model.add(tf.keras.layers.MaxPooling2D())
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(64,activation= 'relu'))
#model.add(Activation('relu'))
model.add(tf.keras.layers.Dense(32,activation= 'linear'))
#model.add(Activation('linear'))
print(model.summary())
Output
Model: "sequential_3"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_7 (Conv2D) (None, 180, 180, 32) 896
max_pooling2d_6 (MaxPooling (None, 90, 90, 32) 0
2D)
conv2d_8 (Conv2D) (None, 90, 90, 64) 8256
max_pooling2d_7 (MaxPooling (None, 45, 45, 64) 0
2D)
conv2d_9 (Conv2D) (None, 45, 45, 64) 16448
max_pooling2d_8 (MaxPooling (None, 22, 22, 64) 0
2D)
flatten_2 (Flatten) (None, 30976) 0
dense_4 (Dense) (None, 64) 1982528
dense_5 (Dense) (None, 32) 2080
=================================================================
Total params: 2,010,208
Trainable params: 2,010,208
Non-trainable params: 0
I have a problem with my machine learning code
here is the model :
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32, 3, activation='relu', input_shape=input_shape),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Conv2D(64, 3, activation='relu'),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Conv2D(128, 3, activation='relu'),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Conv2D(512, 3, activation='relu'),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(1, activation='softmax')])
model.summary()
This is my model.summary() result :
Model: "sequential_32"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_128 (Conv2D) (None, 148, 148, 32) 896
_________________________________________________________________
max_pooling2d_128 (MaxPoolin (None, 74, 74, 32) 0
_________________________________________________________________
conv2d_129 (Conv2D) (None, 72, 72, 64) 18496
_________________________________________________________________
max_pooling2d_129 (MaxPoolin (None, 36, 36, 64) 0
_________________________________________________________________
conv2d_130 (Conv2D) (None, 34, 34, 128) 73856
_________________________________________________________________
max_pooling2d_130 (MaxPoolin (None, 17, 17, 128) 0
_________________________________________________________________
conv2d_131 (Conv2D) (None, 15, 15, 512) 590336
_________________________________________________________________
max_pooling2d_131 (MaxPoolin (None, 7, 7, 512) 0
_________________________________________________________________
flatten_32 (Flatten) (None, 25088) 0
_________________________________________________________________
dense_79 (Dense) (None, 128) 3211392
_________________________________________________________________
dense_80 (Dense) (None, 1) 129
=================================================================
Total params: 3,895,105
Trainable params: 3,895,105
Non-trainable params: 0
And this is the compile setting i use :
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
And this is the fit model code :
EPOCH = 100
history = model.fit(train_data,
steps_per_epoch=len(train_generator),
epochs=EPOCH,
validation_data=val_data,
validation_steps=len(val_generator),
shuffle=True,
verbose = 1)
For the train_data i create use tensorflow tf.data cause i think it is more compatible with tf.keras. this is the tf.data generator function code :
def tf_data_generator(generator, input_shape):
num_class = generator.num_classes
tf_generator = tf.data.Dataset.from_generator(
lambda: generator,
output_types=(tf.float32, tf.float32),
output_shapes=([None
, input_shape[0]
, input_shape[1]
, input_shape[2]]
,[None, num_class])
)
return tf_generator
train_data = tf_data_generator(train_generator, input_shape)
val_data = tf_data_generator(val_generator, input_shape)
actually i got that function from medium.com as source. But i got error when try to train my machine learning code, can someone help me solve the error, this is the error message :
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-16-448faadd058c> in <module>()
6 validation_steps=len(val_generator),
7 shuffle=True,
----> 8 verbose = 1)
9 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
984 except Exception as e: # pylint:disable=broad-except
985 if hasattr(e, "ag_error_metadata"):
--> 986 raise e.ag_error_metadata.to_exception(e)
987 else:
988 raise
ValueError: in user code:
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:855 train_function *
return step_function(self, iterator)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:845 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:1285 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:2833 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:3608 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:838 run_step **
outputs = model.train_step(data)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:797 train_step
y, y_pred, sample_weight, regularization_losses=self.losses)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/compile_utils.py:204 __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/losses.py:155 __call__
losses = call_fn(y_true, y_pred)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/losses.py:259 call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:206 wrapper
return target(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/losses.py:1644 categorical_crossentropy
y_true, y_pred, from_logits=from_logits)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:206 wrapper
return target(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/backend.py:4862 categorical_crossentropy
target.shape.assert_is_compatible_with(output.shape)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py:1161 assert_is_compatible_with
raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (None, 3) and (None, 1) are incompatible
Sorry if my question is confusing, i still new in machine learning field. Thanks for helping me
I guess you intend to get a multi-class classifier, for 3 classes. If that is the case, you wrongly assigned the last layer to a DENSE of size 1. You can solve the issue by replacing this line:
tf.keras.layers.Dense(1, activation='softmax')])
by this:
tf.keras.layers.Dense(3, activation='softmax')])
I'm trying to train DC-CNN model for text classification on a given dataset.
What am I doing wrong here?
Code for Model:
def define_model(length, vocab_size):
# channel 1
inputs1 = Input(shape=(length,))
embedding1 = Embedding(vocab_size, 100)(inputs1)
conv1 = Conv1D(filters=32, kernel_size=4, activation='relu')(embedding1)
drop1 = Dropout(0.5)(conv1)
pool1 = MaxPooling1D(pool_size=1)(drop1)
flat1 = Flatten()(pool1)
# channel 2
inputs2 = Input(shape=(length,))
embedding2 = Embedding(vocab_size, 100)(inputs2)
conv2 = Conv1D(filters=32, kernel_size=6, activation='relu')(embedding2)
drop2 = Dropout(0.5)(conv2)
pool2 = MaxPooling1D(pool_size=1)(drop2)
flat2 = Flatten()(pool2)
merged = concatenate([flat1, flat2])
# interpretation
dense1 = Dense(10, activation='relu')(merged)
outputs = Dense(1, activation='sigmoid')(dense1)
model = Model(inputs=[inputs1, inputs2], outputs=outputs)
# compile
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# summarize
print(model.summary())
return model
model = define_model(length, vocab_size)
model.fit([trainX], array(trainLabels), epochs=10, batch_size=16)
I am getting this error:
AssertionError: in user code:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:806 train_function *
return step_function(self, iterator)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:796 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:1211 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2585 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2945 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:789 run_step **
outputs = model.train_step(data)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:747 train_step
y_pred = self(x, training=True)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:985 __call__
outputs = call_fn(inputs, *args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/functional.py:386 call
inputs, training=training, mask=mask)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/functional.py:517 _run_internal_graph
assert x_id in tensor_dict, 'Could not compute output ' + str(x)
AssertionError: Could not compute output Tensor("dense_17/Sigmoid:0", shape=(None, 1), dtype=float32)
I have tried to reshape the inputs "trainX" and "trainLabels" by using this code but I got the same error
trainX=np.reshape(trainX,(40, 50))
trainLabels=np.reshape(trainLabels,(40, 1))
This is the summary of the model :
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_17 (InputLayer) [(None, 20)] 0
__________________________________________________________________________________________________
input_18 (InputLayer) [(None, 20)] 0
__________________________________________________________________________________________________
embedding_16 (Embedding) (None, 20, 100) 541100 input_17[0][0]
__________________________________________________________________________________________________
embedding_17 (Embedding) (None, 20, 100) 541100 input_18[0][0]
__________________________________________________________________________________________________
conv1d_16 (Conv1D) (None, 17, 32) 12832 embedding_16[0][0]
__________________________________________________________________________________________________
conv1d_17 (Conv1D) (None, 15, 32) 19232 embedding_17[0][0]
__________________________________________________________________________________________________
dropout_16 (Dropout) (None, 17, 32) 0 conv1d_16[0][0]
__________________________________________________________________________________________________
dropout_17 (Dropout) (None, 15, 32) 0 conv1d_17[0][0]
__________________________________________________________________________________________________
max_pooling1d_16 (MaxPooling1D) (None, 17, 32) 0 dropout_16[0][0]
__________________________________________________________________________________________________
max_pooling1d_17 (MaxPooling1D) (None, 15, 32) 0 dropout_17[0][0]
__________________________________________________________________________________________________
flatten_16 (Flatten) (None, 544) 0 max_pooling1d_16[0][0]
__________________________________________________________________________________________________
flatten_17 (Flatten) (None, 480) 0 max_pooling1d_17[0][0]
__________________________________________________________________________________________________
concatenate_8 (Concatenate) (None, 1024) 0 flatten_16[0][0]
flatten_17[0][0]
__________________________________________________________________________________________________
dense_16 (Dense) (None, 10) 10250 concatenate_8[0][0]
__________________________________________________________________________________________________
dense_17 (Dense) (None, 1) 11 dense_16[0][0]
==================================================================================================
Total params: 1,124,525
Trainable params: 1,124,525
Non-trainable params: 0
How can I fix this error Please?
since you have 2 inputs in keras model, so you have to split your trainX in to 2 different arrays, or a tuple of 2 arrays. you cannot give single array as input.