Python - 功能应该是具有高级tf API的`Tensor`s的字典

时间:2018-03-17 23:09:24

标签: python tensorflow machine-learning tensor tensorflow-estimator

我想训练,评估准确性并最终用我的模型进行预测。这是我第一次使用高级API,如tf.estimator。

我从estimator.train(train_input_fn)获得了一个值错误:

'ValueError:功能应该是`Tensor's的字典。给定类型:'

我不确定这里发生了什么。我的模型采用3个输入并从一个神经元产生二进制输出。

在此错误之前,我收到的错误是所请求的形状不等于实际形状,或者沿着这些线条的某些内容。我通过将batchSize减少到1而不是100来修复它。我确信在培训方面这不会很好。

有什么想法吗?继承我的代码:

import tensorflow as tf
import numpy as np
import sys
sys.path.insert(0, '/Users/blairburns/Documents/DeepLearning/BackgroundColourPredictor/Dataset/Testing/')
sys.path.insert(0, '/Users/blairburns/Documents/DeepLearning/BackgroundColourPredictor/Dataset/Training/')
#other files
from TestDataNormaliser import *
from TrainDataNormaliser import *

learning_rate = 0.01
trainingIteration = 15
batchSize = 1
displayStep = 2

#Layers using tf.layers
def get_logits(features):
    l1 = tf.layers.dense(features, 3, activation=tf.nn.relu)
    l2 = tf.layers.dense(l1, 4, activation=tf.nn.relu)
    l3 = tf.layers.dense(l2, 1, activation=None)
    a = l3
    return a


#cost function

def get_loss(a, labels):
    #cross_entropy = tf.reduce_mean(-tf.reduce_sum(y * tf.log(a)))
    return tf.nn.sigmoid_cross_entropy_with_logits(logits=a, labels=labels)
    #cross_entropy = tf.reduce_mean((l3 - y)**2)
    #cross_entropy = -tf.reduce_sum(y*tf.log(a))-tf.reduce_sum((1-y)*tf.log(1-a))


#optimizer

def get_train_op(loss):
    learning_rate = 1e-3
    optimizer = tf.train.RMSPropOptimizer(learning_rate)
    return optimizer.minimize(loss, global_step=tf.train.get_global_step())


#training

####

def get_inputs(feature_data, label_data, batch_size, n_epochs=None, shuffle=True):
    dataset = tf.data.Dataset.from_tensor_slices(
        (feature_data, label_data))

    dataset = dataset.repeat(n_epochs)
    if shuffle:
        dataset = dataset.shuffle(len(feature_data))
    dataset = dataset.batch(batch_size)
    features, labels = dataset.make_one_shot_iterator().get_next()
    return features, labels

def model_fn(features, labels, mode):
    a = get_logits(features)
    loss = get_loss(a, labels)
    train_op = get_train_op(loss)
    predictions = tf.greater(a, 0)
    accuracy = tf.metrics.accuracy(labels, predictions)
    return tf.estimator.EstimatorSpec(
    mode=mode,
    loss=loss,
    train_op=train_op,
    eval_metric_ops={'Accuracy': accuracy},
    predictions=predictions
    )

def train_input_fn():
    return get_inputs(
    trainArrayValues,
    trainArrayLabels,
    batchSize
    )

def eval_input_fn():
    return get_inputs(
    testArrayValues,
    testArrayLabels,
    batchSize,
    n_epochs=1,
    shuffle=False
    )

model_dir = './savedModel'

estimator = tf.estimator.LinearRegressor(feature_columns=[model_fn, model_dir])
#estimator.train(train_input_fn, max_steps=1)
estimator.train(train_input_fn)
estimator.evaluate(eval_input_fn)

1 个答案:

答案 0 :(得分:0)

你的问题就在这一行:

estimator = tf.estimator.LinearRegressor(feature_columns=[model_fn, model_dir])

您需要将feature_columns参数设置为要素列数组。功能列会告知估算人您要为其提供的数据。

看起来您的所有输入数据都是数字的,因此我请致电tf.feature_column.numeric_column来创建您的要素列。文档是here。例如,以下代码创建一个包含x坐标的数字要素列:

xcol = tf.feature_column.numeric_column('x')

如果您的所有估算需求都是x坐标,那么您可以使用以下代码创建估算器:

estimator = tf.estimator.LinearRegressor(feature_columns=[xcol])