Tensorflow:TypeError:' TensorShape'对象不可调用

时间:2018-06-18 16:39:33

标签: python python-3.x numpy tensorflow

我有一个简单的占位符:

input_x = tf.placeholder(name='tensor_a',shape=[2,3,4],dtype=tf.int32)

我想取形状索引并在变量中使用它作为参数:

var_b = tf.get_variable('name_a',shape=[input_x.get_shape()[0],2],dtype=tf.float32,initializer=tf.random_uniform_initializer())

var_c = tf.get_variable('name_b',shape=[input_x.get_shape()[1],2],dtype=tf.float32,initializer=tf.random_uniform_initializer())

完成计划:

import tensorflow as tf
import numpy as np

tf.reset_default_graph()

input_x = tf.placeholder(name='tensor_a',shape=[2,3,4],dtype=tf.int32)

var_b = tf.get_variable('name_a',shape=[input_x.get_shape()[0],2],dtype=tf.float32,initializer=tf.random_uniform_initializer())
var_c = tf.get_variable('name_b',shape=[input_x.get_shape()[1],2],dtype=tf.float32,initializer=tf.random_uniform_initializer())

sum_c=tf.add(var_b,var_c)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(sum_c,feed_dict={input_x:np.random.randint(0,10,[2,3,4])}))

但是我收到了这个错误:

TypeError: 'TensorShape' object is not callable

我如何实现这一目标?

编辑:如果我想重塑一下,我会收到另一个错误:

import tensorflow as tf
import numpy as np

tf.reset_default_graph()

input_x = tf.placeholder(name='tensor_a',shape=[None,None],dtype=tf.int32)
print(input_x.get_shape())

var_b = tf.get_variable('name_a',shape=[4,4],dtype=tf.float32,initializer=tf.random_uniform_initializer())
var_c = tf.get_variable('name_b',shape=[4,4],dtype=tf.float32,initializer=tf.random_uniform_initializer())
vac_d= tf.get_variable('name_d',shape=[var_c.shape[0],60],dtype=tf.float32,initializer=tf.random_uniform_initializer())
reshape_ = tf.reshape(var_c,[input_x.shape[0],input_x.shape[1],-1])
print(reshape_)
sum_c=tf.add(var_b,var_c)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(sum_c,feed_dict={input_x:np.random.randint(0,10,[2,2])}))

错误:

TypeError: Failed to convert object of type <class 'list'> to Tensor. Contents: [Dimension(None), Dimension(None), -1]. Consider casting elements to a supported type.

1 个答案:

答案 0 :(得分:0)

更改

var_b = tf.get_variable('name_a',shape=[input_x.shape()[0],2],dtype=tf.float32,initializer=tf.random_uniform_initializer())
var_c = tf.get_variable('name_b',shape=[input_x.shape()[1],2],dtype=tf.float32,initializer=tf.random_uniform_initializer())

var_b = tf.get_variable('name_a',shape=[input_x.shape[0],2],dtype=tf.float32,initializer=tf.random_uniform_initializer())
var_c = tf.get_variable('name_b',shape=[input_x.shape[1],2],dtype=tf.float32,initializer=tf.random_uniform_initializer())