Python 张量流的问题

Problems with Python tensorflow

我是编程小白,想研究机器学习。我为 Python 使用了 tensorflow。这是代码,使用官方 tensorflow 指南(这里是 https://www.tensorflow.org/guide/basics)编写(但 不是 100% 复制)。训练后我看不到带有结果的最终图表。我尝试了两种训练方法,但都遇到了同样的问题。谁能帮帮我?

import matplotlib as mp
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as pl

mp.rcParams["figure.figsize"] = [20, 10]
precision = 500
x = tf.linspace(-10.0, 10.0, precision)


def y(x): return 4 * np.sin(x - 1) + 3

newY = y(x) + tf.random.normal(shape=[precision])

class Model(tf.keras.Model):
    def __init__(self, units):
        super().__init__()
        self.dense1 = tf.keras.layers.Dense(units = units, activation = tf.nn.relu, kernel_initializer=tf.random.normal, bias_initializer=tf.random.normal)
        self.dense2 = tf.keras.layers.Dense(1)
    
    def __call__(self, x, training = True):
        x = x[:, tf.newaxis]
        x = self.dense1(x)
        x = self.dense2(x)
        return tf.squeeze(x, axis=1)

model = Model(164)

pl.plot(x, y(x), label = "origin")
pl.plot(x, newY, ".", label = "corrupted")
pl.plot(x, model(x), label = "before training")

"""                                                     The first method
vars = model.variables
optimizer = tf.optimizers.SGD(learning_rate = 0.01)

for i in range(1000):
    with tf.GradientTape() as tape:
        prediction = model(x)
        error = (newY-prediction)**2
        mean_error = tf.reduce_mean(error)
    gradient = tape.gradient(mean_error, vars)
    optimizer.apply_gradients(zip(gradient, vars))
"""

model.compile(loss = tf.keras.losses.MSE, optimizer = tf.optimizers.SGD(learning_rate = 0.01))
model.fit(x, newY, epochs=100,batch_size=32,verbose=0)

pl.plot(x, model(x), label = "after training")
pl.legend()
pl.show()

据我所见,你的第三张图和第四张图是一样的。他们是 pl.plot(x, model(x), label = "before training")pl.plot(x, model(x), label = "after training")可以看出两个图的x-axis和y-axis数据是一样的

希望我的回答对您有所帮助!

我复制了你的代码并进行了调查。你的模型 returns 训练期间的 NaN 损失,我删除了内核和偏差初始值设定项并且它有效。现在我不知道你的初始化有什么问题。似乎有些权重是用 NaN 初始化的,然后使预测变为 NaN,因此您无法绘制它们。

更新:使用初始化模块(如 tensorflow.initializerstensorflow.keras.initializers,而不是 tensorflow.random)。例如,使用 kernel_initializer=tf.initializers.random_normal 而不是您拥有的