tensorflow 2 api regression tensorflow.python.framework.ops.EagerTensor'对象不可调用


逆转的风
2025-03-12 01:47:37 (27天前)


我试图使用tensorflow 2 api实现多元回归。

导入张量流为tf
将pandas导入为pd
导入numpy为np
将matplotlib.pyplot导入为plt

df = pd.DataFrame({‘A’:np ….

2 条回复
  1. 0# 甲基蓝 | 2019-08-31 10-32



    2)你肯定需要使用GradientTape。



    看看

    有效的TF2指南



    1)像这样的东西:




    1. import tensorflow as tf
      import numpy as np

    2. print(“TensorFlow version: {}”.format(tf.version))
      print(“Eager execution: {}”.format(tf.executing_eagerly()))

    3. x = np.array([
      [100, 105.4, 108.3, 111.1, 113, 114.7],
      [11, 11.8, 12.3, 12.8, 13.1, 13.6],
      [55, 56.3, 57, 58, 59.5, 60.4]
      ])

    4. y = np.array([4000, 4200.34, 4700, 5300, 5800, 6400])

    5. class Model(object):
      def init(self, x, y):

    6.     # Initialize variable to (5.0, 0.0)
    7.     # In practice, these should be initialized to random values.
    8.     self.W = tf.Variable(tf.random.normal((len(x), len(x[0]))))
    9.     self.b = tf.Variable(tf.random.normal((len(y),)))
    10. def __call__(self, x):
    11.     return self.W * x + self.b
    12. def loss(predicted_y, desired_y):
      return tf.reduce_sum(tf.square(predicted_y - desired_y))

    13. optimizer = tf.optimizers.Adam(0.1)

    14. noinspection PyPep8Naming

      def train(model, inputs, outputs):
      with tf.GradientTape() as t:
      current_loss = loss(model(inputs), outputs)
      grads = t.gradient(current_loss, [model.W, model.b])
      optimizer.apply_gradients(zip(grads,[model.W, model.b]))
      print(current_loss)

    15. model = Model(x, y)

    16. for i in range(10000):

    17. # print(model.b.numpy())
    18. train(model,x,y)
    19. </code>

登录 后才能参与评论