简体   繁体   中英

Tensorflow Value Error: Input is incompatible with the layer

I'm currently try to train my first model with tensorflow. For this I have labeled data:

tlabelstrain = ops.convert_to_tensor(label_train_data, dtype=dtypes.int32)
tdatatrain = ops.convert_to_tensor(data_train_data)

labeled_train_data = tf.data.Dataset.from_tensor_slices((tdatatrain, tlabelstrain))

The training data is from 499 files, which contain 300*13 features. Each of these 499 files has a label within [1,2,3,4,5,6,7].

If I do print(labeled_train_data) I get this:

<TensorSliceDataset shapes: ((300, 13), ()), types: (tf.float64,tf.int32)>

Then I tried to create a model and train it:

model = tf.keras.Sequential()

model.add(layers.LSTM(128, input_shape=(300, 13), activation='relu', return_sequences=True))

model.add(layers.Dropout(0.2))
model.add(layers.LSTM(128, activation='relu'))
model.add(layers.Dropout(0.2))
model.add(layers.Dense(32, activation='relu'))
model.add(layers.Dropout(0.2))

model.add(layers.Dense(7, activation='softmax'))

model.compile(loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
              optimizer=tf.keras.optimizers.Adam(1e-4),
              metrics=['accuracy'])

model.fit(labeled_train_data, epochs=3)

If I run it, I get a Value Error:

ValueError: Input 0 of layer sequential_12 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [300, 13]

I think I messed something up with the shape of the input but I dont know where I did it

Thanks for your help!

I think you left the batch_size there. Does it work when you specify it by using:

model.add(layers.LSTM(128, input_shape=(None, 300, 13), activation='relu', return_sequences=True))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM