【问题标题】:An error when declaring the variable placeholder声明变量占位符时出错
【发布时间】:2021-07-27 14:59:37
【问题描述】:

我正在使用 Pycharm、tensorflow 2.4 和 python v3.8.5 构建示例神经网络。运行此命令时:

X = tf.placeholder("float", [None, num_input])   #num_input is the sized of input vector 

我收到类似这样的错误:

raise TypeError("Error converting %s to a TensorShape: %s." % (arg_name, e))

TypeError: Error converting shape to a TensorShape: Dimension value must be integer or None or have an __index__ method, got value '8.0' with type '<class 'numpy.float64'>'.

那个错误的问题是什么?提前致谢

【问题讨论】:

    标签: python tensorflow neural-network pycharm


    【解决方案1】:

    此行 (tf.placeholder) 已为 tensorflow 1 启用,但您已安装 tensorflow2。禁用 Tf2 并在 Tf1 上运行您的后端。

    import tensorflow.compat.v1 as tf
    tf.disable_v2_behavior()
    X = tf.placeholder("float", [None, 8])
    
    print(X)
    <tf.Tensor 'Placeholder:0' shape=(?, 8) dtype=float32>
    

    【讨论】:

    • 谢谢,我正在使用这两行; import tensorflow.compat.v1 as tf tf.disable_v2_behavior() 顺便说一句,我认为 Tf1 与 Tf2 相比更稳定,我试图通过安装 TF1 而不是 Tf2 来解决这个问题。
    • 编码愉快!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    相关资源
    最近更新 更多