Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将形状为(15000,250)的目标阵列传递为形状(无,1)的输出,同时将其用作损失“二进制交叉熵”。我该怎么办?_Python_Tensorflow_Machine Learning_Keras_Deep Learning - Fatal编程技术网

Python 将形状为(15000,250)的目标阵列传递为形状(无,1)的输出,同时将其用作损失“二进制交叉熵”。我该怎么办?

Python 将形状为(15000,250)的目标阵列传递为形状(无,1)的输出,同时将其用作损失“二进制交叉熵”。我该怎么办?,python,tensorflow,machine-learning,keras,deep-learning,Python,Tensorflow,Machine Learning,Keras,Deep Learning,我已经创建了一个模型,但由于目标数组形状和输出形状的原因,我无法运行它。我试着训练它,但不确定如何从错误中吸取教训 错误: --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-9-4c7f3cb9ee70>

我已经创建了一个模型,但由于目标数组形状和输出形状的原因,我无法运行它。我试着训练它,但不确定如何从错误中吸取教训

错误:

---------------------------------------------------------------------------
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)
改变

y_val = train_data[:10000]
y_train = train_data[10000:]


你的火车是什么形状的?代码上写着。10000这不是你的错误所说的。尝试printy_train.shape并发布结果。我不能,我得到上面的错误。我使用的是谷歌Colab。我认为@thushv89意味着你应该在出现错误的那一行之前打印出来。请解释原因。看见
y_val = train_labels[:10000]
y_train = train_labels[10000:]