【发布时间】:2019-05-03 19:49:48
【问题描述】:
假设我的输入数据 x 的形状为 (2000, 2),其中 2000 是样本数,2 是特征数。
所以对于这个输入数据,我可以像这样设置一个占位符: x = tf.placeholder(tf.float32, shape=[None, 2], name='features')
我的问题是,如果我转置输入数据 x,使形状现在为 (2, 2000),其中 2000 仍然是样本数,我将如何更改 tf.placeholder 中的“形状”参数?
我尝试设置 shape=[2, None],但我得到了一个错误。 “形状”参数中的第一个元素是否总是必须是“无”?
这里我得到的错误是:“ValueError:应该定义Dense的输入的最后一个维度。找到None。”
import tensorflow as tf
# Binary Classifier Implementation
# Training data
x_train = np.transpose(X) #shape=(2, 2000)
y_train = np.hstack((np.zeros((1, 1000)),np.zeros((1, 1000)) + 1)) #shape=(1, 2000)
# Variables
x = tf.placeholder(tf.float32, shape=[2, None], name='features')
y_ = tf.placeholder(tf.int64, shape=[1, None], name='labels')
h1 = tf.layers.dense(inputs=x, units=50, activation=tf.nn.relu) #one hidden layer with 50 neurons
y = tf.layers.dense(inputs=h1, units=1, activation=tf.nn.sigmoid) #one output layer with 1 neuron
# Functions
#loss
cross_entropy = tf.losses.sigmoid_cross_entropy(multi_class_labels=y_, logits=y)
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(cross_entropy)
# Initializer
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for i in range(1000):
sess.run([cross_entropy], feed_dict={x: x_train, y_: y_train})
【问题讨论】:
标签: python tensorflow neural-network