【问题标题】:Convert code from previous TensorFlow version to new TensorFlow 2.0将代码从以前的 TensorFlow 版本转换为新的 TensorFlow 2.0
【发布时间】:2020-12-28 15:13:37
【问题描述】:

如何将此代码转换为最新的 TensorFlow 2.0 版?

def create_placeholders(n_x,n_y):

    X=t.placeholder(tf.float32,[n_x,None],name='X')
    Y=tf.placeholder(tf.float32,[n_y,None],name='Y')
    
    return X,Y

【问题讨论】:

  • 如果您认为答案有帮助,请采纳答案

标签: python tensorflow anaconda


【解决方案1】:

不再需要占位符了。你可以简单地假设你有一个张量变量

def create_placeholders(n_x,n_y):

   X=tf.Variable(dtype=tf.float32,initial_value=[n_x,None],name='X',dtype=tf.flot32)
   Y=tf.Variable(dtype=tf.float32,initial_value=[n_y,None],name='Y',dtype=tf.flot32)

   return X,Y

【讨论】:

  • 感谢您的回复!但是,当我调用函数时,X,Y=create_placeholders(12288,6) 我收到一个值错误 ValueError: Can't convert Python sequence with mixed types to Tensor。我应该如何解决这个问题?
  • 您需要向您的代码提供错误信息。
  • 强制类型为 float32。我已经更新了答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-08
  • 2021-05-23
  • 2018-03-31
  • 2019-10-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多