ValueError: `logits` and `labels` must have the same shape, received ((None, 2) vs (None, 1)).
我使用二元交叉熵将电影评论分为正面或负面。所以,当我试图用tensorflow估计器包裹我的keras模型时,我得到了错误:
import tensorflow as tf
import tensorflow as tf
genrator=tf.keras.preprocessing.image.ImageDataGenerator()
train=genrator.flow_from_directory('data/',class_mode='binary',
batch_size=30,target_size=(128,128))
test=genrator.flow_from_directory('test_data/',class_mode='binary',
batch_size=30,target_size=(128,128))
import os
from PIL import Image
folder_path = 'data/'
extensions = []
for fldr in os.listdir(folder_path):
sub_folder_path = os.path.join(folder_path, fldr)
for filee in os.listdir(sub_folder_path):
file_path = os.path.join(sub_folder_path, filee)
print('** Path: {} **'.format(file_path), end="r", flush=True)
im = Image.open(file_path)
rgb_im = im.convert('RGB')
if filee.split('.')[1] not in extensions:
extensions.append(filee.split('.')[1])
model=tf.keras.models.Sequential([
# tf.keras.layers.ZeroPadding2D((1,1),input_shape=(128,128,1)),
tf.keras.layers.Conv2D(32,(3,3),activation='ELU',input_shape=(128,128,1)),
tf.keras.layers.MaxPool2D((2,2)),
tf.keras.layers.Conv2D(64,(3,3),activation='relu'),
tf.keras.layers.MaxPool2D((2,2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128,activation='relu'),
tf.keras.layers.Dense(256,activation='relu'),
tf.keras.layers.Dense(2,activation='sigmoid'),
])
model.compile(optimizer=tf.keras.optimizers.Adam(),
loss='binary_crossentropy',metrics=['accuracy'])
model.fit_generator(train, steps_per_epoch=len(train), validation_data=test, validation_steps=len(test), epochs=10)
由于您使用的是sigmoid函数,这是一个二进制分类问题,因此您的最后一层,即属于正类(或logit(的分数,应该有一个节点。它可能可以像这样将二改一:
tf.keras.layers.Dense(1,activation='sigmoid')
错误说每个预测有两个分数,但你只给了我一个标签(0或1(。