简体   繁体   English

为什么我需要在TensorFlow中输入数据的字典数据类型

[英]Why do I need a dictionary datatype for input data in TensorFlow

(X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data()
X_train = X_train.astype(np.float32).reshape(-1, 28*28) / 255.0
X_test = X_test.astype(np.float32).reshape(-1, 28*28) / 255.0
y_train = y_train.astype(np.int32)
y_test = y_test.astype(np.int32)
X_valid, X_train = X_train[:5000], X_train[5000:]
y_valid, y_train = y_train[:5000], y_train[5000:]

dnn_clf = tf.estimator.DNNClassifier(hidden_units=[300,100], n_classes=10,
feature_columns=feature_cols)

input_fn = tf.estimator.inputs.numpy_input_fn(
x={"X": X_train}, y=y_train, num_epochs=40, batch_size=50, shuffle=True)

dnn_clf.train(input_fn=input_fn)

My question is: What's the difference between 我的问题是:两者之间有什么区别

x={"X": X_train}, y=y_train, num_epochs=40, batch_size=50, shuffle=True)

and

x=X_train, y=y_train, num_epochs=40, batch_size=50, shuffle=True)

Why can't I just use a list X_train for the input x , but have to convert X_train to a dictionary x={"X": X_train} 为什么我不能X_train输入x使用列表 X_train ,而必须将X_train转换为字典 x={"X": X_train}

Why I don't need to do the same thing for y ? 为什么我不需要为y做同样的事情?

According to documentation TensorFlow 1.11 for numpy_input_fn , you can use both approaches. 根据用于numpy_input_fn的文档TensorFlow 1.11,可以使用两种方法。 But I've tested it for TensorFlow 1.9 and it didn't work. 但是我已经在TensorFlow 1.9上对其进行了测试,但是它没有用。 So, it seems that this functionality was added later. 因此,似乎此功能是在以后添加的。

If I understood, another question was "why x parameter is a map and y is just an array". 据我了解,另一个问题是“为什么x参数是一个映射而y只是一个数组”。 The thing is that model can have multiple input feature columns/tensors, but the output tensor can be only one. 问题是模型可以具有多个输入要素列/张量,但输出张量只能是一个。 That's true for, at least, standard estimators and for general use-cases. 至少对于标准估计器和一般用例而言,这是正确的。 So, you need to somehow specify multiple inputs and the most simple way is just to pass a map. 因此,您需要以某种方式指定多个输入,而最简单的方法就是传递地图。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM