简体   繁体   English

Tensorflow 估计器 ValueError:logits 和标签必须具有相同的形状 ((?, 1) vs (?,))

[英]Tensorflow estimator ValueError: logits and labels must have the same shape ((?, 1) vs (?,))

I'm classifying movie reviews as positive or negative using binary crossentropy.我使用二元交叉熵将电影评论分类为正面或负面。 So, when I'm trying to wrap my keras model with tensorflow estimator, I get the error:所以,当我试图用 tensorflow 估算器包装我的 keras model 时,我得到了错误:

Tensorflow estimator ValueError: logits and labels must have the same shape ((?, 1) vs (?,))

I'm using sigmoid activation as my last layer, guess I'm missing something trivial here.我使用 sigmoid 激活作为我的最后一层,我想我在这里遗漏了一些微不足道的东西。 Any help?有什么帮助吗?

from tensorflow import keras
import tensorflow as tf
print("Tensorflow {} loaded".format(tf.__version__))
import numpy as np

keras.__version__
from keras.datasets import imdb

(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)
def vectorize_sequences(sequences, dimension=10000):
    # Create an all-zero matrix of shape (len(sequences), dimension)
    results = np.zeros((len(sequences), dimension))
    for i, sequence in enumerate(sequences):
        results[i, sequence] = 1.  # set specific indices of results[i] to 1s
    return results.astype('float32')

# Our vectorized training data
x_train = vectorize_sequences(train_data)

# Our vectorized test data
x_test = vectorize_sequences(test_data)

# Our vectorized labels
y_train = np.asarray(train_labels).astype('float32')
y_test = np.asarray(test_labels).astype('float32')

x_val = x_train[:10000]
partial_x_train = x_train[10000:]
y_val = y_train[:10000]
partial_y_train = y_train[10000:]

model = keras.models.Sequential()
model.add(keras.layers.Dense(16, activation='relu', input_shape=(10000,), name='reviews'))
model.add(keras.layers.Dense(16, activation='relu'))
model.add(keras.layers.Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['accuracy'])
estimator_model = keras.estimator.model_to_estimator(keras_model=model)

def input_function(features,labels=None,shuffle=False,epochs=None,batch_size=None):
    input_fn = tf.estimator.inputs.numpy_input_fn(
        x={"reviews_input": features},
        y=labels,
        shuffle=shuffle,
        num_epochs=epochs,
        batch_size=batch_size
    )
    return input_fn

estimator_model.train(input_fn=input_function(partial_x_train, partial_y_train, True,20,512))
score = estimator_model.evaluate(input_function(x_val, labels=y_val))
print(score)

You should reshape your labels as 2d-tensor (the first dimension will be the batch dimension and the second the scalar label):您应该将标签重塑为二维张量(第一个维度将是批次维度,第二个维度是标量标签):

# Our vectorized labels
y_train = np.asarray(train_labels).astype('float32').reshape((-1,1))
y_test = np.asarray(test_labels).astype('float32').reshape((-1,1))

If you're doing a binary classification, make sure that that your last Dense layer has just (None, 1) in its shape, not None, 2)如果您正在进行二元分类,请确保最后一个 Dense 层的形状只有 (None, 1),而不是 None, 2)

tf.keras.layers.Dense(1, activation="sigmoid") # binary activation output

Check your network using model.summary()使用 model.summary() 检查您的网络

You eventually need to thin down the network to have the same outputs as your classes.您最终需要精简网络以获得与您的类相同的输出。 For example, doing OCR for numbers needs and final output of Dense(10) (for numbers 0 to 9).例如,对数字需求和 Dense(10) 的最终输出(数字 0 到 9)进行 OCR。

For example characterizing dogs vs. cats.例如表征狗与猫。 The final layer has to have two outputs (0-dog, 1-cat)最后一层必须有两个输出(0-dog,1-cat)

We can solve this problem by matching the output with the dimension of the label by adding a Flatten layer after the Dense layer:我们可以通过在 Dense 层之后添加一个 Flatten 层,将输出与标签的维度进行匹配来解决这个问题:

model.add(Flatten())

or by adding:或通过添加:

model.add(GlobalAveragePooling2D())

See this GitHub issue for full details 有关完整详细信息,请参阅此 GitHub 问题

If you're doing binary cross-entropy, then your dataset probably has 2 classes and the error is coming because your label vectors (both in testing and training) have the form [0,1,0,1,1,1,0,0,1,...].如果你在做二元交叉熵,那么你的数据集可能有 2 个类并且错误即将到来,因为你的标签向量(在测试和训练中)具有 [0,1,0,1,1,1,0 ,0,1,...]。 To one-hot encode binary labels, the following function can be used: Labels = tf.one_hot(Labels, depth=2)要对二进制标签进行单热编码,可以使用以下函数: Labels = tf.one_hot(Labels, depth=2)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:logits 和标签必须具有相同的形状 ((None, 5) vs (None, 1)) - ValueError: logits and labels must have the same shape ((None, 5) vs (None, 1)) ValueError:logits 和标签必须具有相同的形状 ((1, 7, 7, 2) vs (1, 2)) - ValueError: logits and labels must have the same shape ((1, 7, 7, 2) vs (1, 2)) ValueError:logits 和标签必须具有相同的形状 ((1, 21) vs (21, 1)) - ValueError: logits and labels must have the same shape ((1, 21) vs (21, 1)) TENSORFLOW 找不到解决方案:ValueError: logits and labels must have the same shape ((None, 1) vs (None, 2, 2)) - TENSORFLOW Can't find a solution for: ValueError: logits and labels must have the same shape ((None, 1) vs (None, 2, 2)) ValueError:logits 和标签必须具有相同的形状 ((None, 6, 8, 1) vs (None, 1)) - ValueError: logits and labels must have the same shape ((None, 6, 8, 1) vs (None, 1)) ValueError:logits 和标签必须具有相同的形状 ((32, 1) vs (32, 2)) - ValueError: logits and labels must have the same shape ((32, 1) vs (32, 2)) ValueError:logits 和标签必须具有相同的形状 ((None, 1) vs (None, 2)) - ValueError: logits and labels must have the same shape ((None, 1) vs (None, 2)) ValueError:logits 和标签必须具有相同的形状 ((None, 2) vs (None, 1)) - ValueError: logits and labels must have the same shape ((None, 2) vs (None, 1)) ValueError:logits 和标签必须具有相同的形状 ((None, 4) vs (None, 1)) - ValueError: logits and labels must have the same shape ((None, 4) vs (None, 1)) Tensorflow ValueError:logits 和标签必须具有相同的形状((无,2)与(无,1)) - Tensorflow ValueError: logits and labels must have the same shape ((None, 2) vs (None, 1))
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM