SparseSoftmaxCrossEntropyWithLogits错误的形状

时间:2019-01-25 19:16:37

标签: python python-3.x tensorflow keras

作为标题,我得到的错误是:

ValueError: Expected scalar shape for SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits:0, saw shape: (52692,).

我要传递的形状: logits-[52692,4]-等级2 标签-[52692]-排名1

有4个课程

这是我将输入基本上传递给(特征,标签)元组的方式

def _input_fn(training_dir, training_filename):
    def getFeatureLabelOH(file):
        features, labels = csvGetColumns(file)
        count = len(features)
        # converting features and labels to integers
        featuresVec, labelsVec = convCharToVec(features, alphabetDict, maxFeatureLen), \
                                 [conventions[label] for label in labels]
        featuresVec = tf.convert_to_tensor(featuresVec, dtype=tf.int32)
        labelsVec = tf.convert_to_tensor(labelsVec, dtype=tf.int32)
        labelsVec = tf.reshape(labelsVec, [-1])
        return {"featuresVec": featuresVec, "labelsVec": labelsVec, "count": count}
    data = getFeatureLabelOH(os.path.join(training_dir, 
    return (data["featuresVec"], data["labelsVec"])

还有我的实际模型

def model_fn(features, labels, mode, params):
    net = keras.layers.Embedding(alphabetLen + 1, 8, input_length=maxFeatureLen)(features)
    net = keras.layers.LSTM(12)(net)
    logits = keras.layers.Dense(len(conventions), activation=tf.nn.softmax)(net) #output
    loss = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels, logits=logits)
    train_op = tf.contrib.layers.optimize_loss(
        loss=loss,
        global_step=tf.train.get_global_step(),
        learning_rate=0.001,
        optimizer="AdamOptimizer")
    eval_metric_ops = {}
    return tf.estimator.EstimatorSpec(
        mode=mode,
        loss=loss,
        train_op=train_op,
        eval_metric_ops=eval_metric_ops)

0 个答案:

没有答案