问题描述
我正在尝试学习tensorflow,并且正在遵循一个演示教程( https://www.tensorflow.org/tutorials/keras/basic_text_classification )
I am trying to learn tensorflow, and I was following a demo tutorial (https://www.tensorflow.org/tutorials/keras/basic_text_classification)
错误报告告诉我
检查目标时出错:预期density_1的形状为(1,),但数组的形状为(256,)"
"Error when checking target: expected dense_1 to have shape (1,) but got array with shape (256,)"
有人可以向我解释为什么这行不通吗?
Can someone explain to me why this won't work?
train_data = keras.preprocessing.sequence.pad_sequences(train_data,
value=word_index["<PAD>"],
padding='post',
maxlen=256) #max length
test_data = keras.preprocessing.sequence.pad_sequences(test_data,
value=word_index["<PAD>"],
padding='post',
maxlen=256)
vocal_size = 10000
model = keras.Sequential()
model.add(keras.layers.Embedding(vocal_size,16))
model.add(keras.layers.GlobalAveragePooling1D())
model.add(keras.layers.Dense(16,activation=tf.nn.relu))
model.add(keras.layers.Dense(1,activation=tf.nn.sigmoid))
model.compile(optimizer=tf.train.AdamOptimizer(),
loss='binary_crossentropy',
metrics=['accuracy'])
x_val = train_data[:10000]
partial_x_train = train_data[10000:]
y_val = train_data[:10000]
partial_y_train = train_data[10000:]
history = model.fit(partial_x_train, partial_y_train, epochs=40, batch_size=512, validation_data=(x_val, y_val), verbose=1)
推荐答案
这些行中有错误
y_val = train_data[:10000]
partial_y_train = train_data[10000:]
但是本教程说应该是
y_val = train_labels[:10000]
partial_y_train = train_labels[10000:]
train_data
代表每个书面评论,而train_labels
代表评论是正面还是负面.您希望模型在书面评论为正面或负面时学习.
train_data
represents each written review, and train_labels
represents whether the reviews are positive or negative. You want your model to learn when a written review is positive or negative.
这篇关于检查目标时出错:预期density_1的形状为(1,),但数组的形状为(256,)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!