微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Tensorflow中踩过的坑

1.程序报错:Feed的值不能是一个tensor,只能是标量、字符串、列表、数组等,所以不能用tf.reshape, 应该使用np.reshape。

    with tf.Session() as sess:
        tf.global_variables_initializer().run()
        v_x = tf.reshape(mnist.validation.images, [mnist.validation.num_examples, 28, 28, 1])
        t_x = tf.reshape(mnist.test.images, [mnist.test.num_examples, 28, 28, 1])
        validate_Feed = {x: v_x, y: mnist.validation.labels}
        test_Feed = {x: t_x, y: mnist.test.labels}

TypeError: The value of a Feed cannot be a tf.Tensor object. 
Acceptable Feed values include Python scalars, strings, lists, numpy ndarrays, or TensorHandles.

2.pool_shape的第一维是None,这是为了便于调整batch大小,但是这样的话tf.reshape无法将输出的矩阵转换为向量。可以直接使用slim.flatten()函数进行转换,不需要读取shape的大小。

# 将输出矩阵拉伸成一个向量
    pool_shape = pool2.get_shape().as_list()    # 只有tensor能用get_shape,as_list将元组转换为列表
    nodes = pool_shape[1]*pool_shape[2]*pool_shape[3]
    reshaped = tf.reshape(pool2, [pool_shape[0], nodes])

TypeError: Failed to convert object of type <class 'list'> to Tensor.
    pool_shape = pool2.get_shape().as_list()   
    nodes = pool_shape[1]*pool_shape[2]*pool_shape[3]
    reshaped = tf.contrib.slim.flatten(pool2)

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐