我已经创建了一个模型,但是由于目标数组的形状和输出形状而无法运行它。我正在尝试对其进行培训,但不确定如何从错误中找出答案。
错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-9-4c7f3cb9ee70> in <module>()
44 y_train = train_data[10000:]
45
---> 46 fitModel = model.fit(x_train, y_train, epochs=5, batch_size=512, validation_data=(x_val, y_val), verbose=1)
47
48 result = model.evaluate(test_data, test_labels)
3 frames
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/training_utils.py in check_loss_and_target_compatibility(targets, loss_fns, output_shapes)
739 raise ValueError('A target array with shape ' + str(y.shape) +
740 ' was passed for an output of shape ' + str(shape) +
--> 741 ' while using as loss `' + loss_name + '`. '
742 'This loss expects targets to have the same shape '
743 'as the output.')
ValueError: A target array with shape (15000, 250) was passed for an output of shape (None, 1) while using as loss `binary_crossentropy`. This loss expects targets to have the same shape as the output.
我应该得到准确性和执行时间的输出。我尝试更改输出层的值,但对我来说根本不起作用。
我的代码:
import tensorflow as tf
from tensorflow import keras
import numpy as np
import time
start_time = time.time()
data = tf.keras.datasets.imdb
(train_data, train_labels), (test_data, test_labels) = data.load_data(num_words=7500)
word_index = data.get_word_index()
word_index = {k:(v+3) for k, v in word_index.items()}
word_index["<PAD>"] = 0
word_index["<START>"] = 1
word_index["<UNKNOWN>"] = 2
word_index["<UNUSED>"] = 3
reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])
train_data = keras.preprocessing.sequence.pad_sequences(train_data, value= word_index["<PAD>"], padding="post", maxlen=250)
test_data = keras.preprocessing.sequence.pad_sequences(train_data, value= word_index["<PAD>"], padding="post", maxlen=250)
def decode_review(text):
return " ".join([reverse_word_index.get(i, "?") for i in text])
model = keras.Sequential()
model.add(keras.layers.Embedding(10000, 16))
model.add(keras.layers.GlobalAveragePooling1D())
model.add(keras.layers.Dense(16, activation="relu"))
model.add(keras.layers.Dense(1, activation="sigmoid"))
model.summary()
model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
x_val = train_data[:10000]
x_train = train_data[10000:]
y_val = train_data[:10000]
y_train = train_data[10000:]
fitModel = model.fit(x_train, y_train, epochs=5, batch_size=512, validation_data=(x_val, y_val), verbose=1)
result = model.evaluate(test_data, test_labels)
print(results)
time1 = time.time() - start_time
start_time = time.time()
print(float(test_acc1) / 1)
print(float(time1) / 1)
答案 0 :(得分:0)
更改
y_val = train_data[:10000]
y_train = train_data[10000:]
到
y_val = train_labels[:10000]
y_train = train_labels[10000:]