【问题标题】:Understanding the shape of tensorflow placeholders理解 tensorflow 占位符的形状
【发布时间】:2018-01-13 16:10:52
【问题描述】:

我正在阅读this code,我想了解它的实现。


我想知道的第一件事是,一些张量对象(占位符)的形状是什么,例如x_initxsh_inity_inity_sample、等等。

我写了一行代码,比如print(xs.shape),但它不起作用。

如何理解这些参数(张量)的形状?我可以在 NumPy 中编写类似以下的内容吗?


定义这些张量的代码部分如下所示:

x_init = tf.placeholder(tf.float32, shape=(args.init_batch_size,) + obs_shape)

xs = [tf.placeholder(tf.float32, shape=(args.batch_size, ) + obs_shape)
      for i in range(args.nr_gpu)]


# if the model is class-conditional we'll set up label placeholders +
# one-hot encodings 'h' to condition on if args.class_conditional:

num_labels = train_data.get_num_labels()
y_init = tf.placeholder(tf.int32, shape=(args.init_batch_size,))
h_init = tf.one_hot(y_init, num_labels)
y_sample = np.split(
    np.mod(np.arange(args.batch_size * args.nr_gpu), num_labels), args.nr_gpu)
h_sample = [tf.one_hot(tf.Variable(
    y_sample[i], trainable=False), num_labels) for i in range(args.nr_gpu)]

【问题讨论】:

  • 请花时间自己格式化代码。你已经有了一些声誉,你应该知道这个网站上的事情是如何运行的(发布一个格式正确的问题就是其中之一)。
  • 您可以用 numpy 编写代码,但如何计算损失函数对所有参数的梯度?还是你已经这样做了?
  • @kmario23 我不是说用 numpy 编写整个程序。只是在 numpy 中对 xs、ys、hs 等的这些定义,所以我可以理解它们为什么使用这些参数。

标签: python python-3.x numpy tensorflow


【解决方案1】:

形状由不同的命令行参数组装而成:

  • obs_shape 是输入图像的形状,例如,(32, 32, 3)
  • args.init_batch_sizeargs.batch_size 是来自命令行的值。例如,可能是 3040

那么x_init的形状是init_batch_sizeobs_shape的串联:(30, 32, 32, 3)。对应地,xs中每一项的shape为(40, 32, 32, 3)

您无法评估xs.shape,因为xs 是占位符的列表。您可以改为评估 xs[0].shape

y_sampleh_sample 也是张量列表。第一个包含(batch_size, num_labels) 张量,第二个包含(num_labels, )

【讨论】:

  • 非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-25
  • 1970-01-01
相关资源
最近更新 更多