Python TensorFlow 2.0 reduce_mean方法带来空形状张量

Python TensorFlow 2.0 reduce_mean方法带来空形状张量,python,tensorflow,machine-learning,Python,Tensorflow,Machine Learning,我是TensorFlow的新手,我见过该代码在版本2.x中使用兼容方法实现,如.compat.v1。工作正常,达到了预期效果 将代码从2.x版修改为1.x版 from sklearn.preprocessing import StandardScaler import numpy as np from sklearn.datasets import fetch_california_housing housing = fetch_california_housing() m, n = hous

我是TensorFlow的新手,我见过该代码在版本
2.x
中使用兼容方法实现,如
.compat.v1
。工作正常,达到了预期效果

将代码从2.x版修改为1.x版

from sklearn.preprocessing import StandardScaler
import numpy as np
from sklearn.datasets import fetch_california_housing

housing = fetch_california_housing()
m, n = housing.data.shape

housing_data_plus_bias = np.c_[np.ones((m, 1)), housing.data]

std_scaler = StandardScaler()
scaled_housing_data_plus_bias = std_scaler.fit_transform(housing_data_plus_bias)

n_epochs = 1000
learning_rate = 0.01

X = tf.constant(scaled_housing_data_plus_bias, dtype=tf.float32, name="X")
y = tf.constant(housing.target.reshape(-1, 1), dtype=tf.float32, name="y")
theta = tf.Variable(tf.compat.v1.random_uniform([n + 1, 1], -1.0, 1.0), name="theta")
y_pred = tf.matmul(X, theta, name="predictions")
error = y_pred - y
mse = tf.reduce_mean(tf.square(error), name="mse")
gradients = 2/m * tf.matmul(tf.transpose(X), error)
training_op = tf.compat.v1.assign(theta, theta - learning_rate * gradients)

init = tf.compat.v1.global_variables_initializer()

with tf.compat.v1.Session() as sess:
    sess.run(init)

    for epoch in range(n_epochs):
        if epoch % 100 == 0:
            print(f"Epoch {epoch} -> MSE = {mse.eval()}")
        sess.run(training_op)

    best_theta = theta.eval()

print(best_theta)
import numpy as np
import tensorflow as tf
from sklearn.datasets import fetch_california_housing
from sklearn.preprocessing import StandardScaler

housing = fetch_california_housing()
m, n = housing.data.shape

housing_data_plus_bias = np.c_[np.ones((m, 1)), housing.data]

std_scaler = StandardScaler()
scaled_housing_data_plus_bias = std_scaler.fit_transform(housing_data_plus_bias)

X = tf.constant(scaled_housing_data_plus_bias, dtype=tf.float32, name="X")
Y = tf.constant(housing.target.reshape(-1, 1), dtype=tf.float32, name="Y")
theta = tf.Variable(tf.random.uniform([n + 1, 1], -1.0, 1.0), name="theta")

@tf.function
def gradiente_descendente(x, y, n_epochs, learning_rate):
    for epoch in range(n_epochs):
        y_pred = tf.matmul(x, theta, name="predictions")
        error = y_pred - y
        mse = tf.reduce_mean(tf.square(error), name="mse")
        gradients = 2/m * tf.matmul(tf.transpose(x), error)
        theta.assign_add(- learning_rate * gradients, name="theta")
        if epoch % 100 == 0:
            print(f"Epoch {epoch} -> MSE = {mse}")
    return theta

result = gradiente_descendente(X, Y, n_epochs=1000, learning_rate=0.01)
print(result.numpy())
输出1

Epoch 0 -> MSE = 7.109368801116943
Epoch 100 -> MSE = 4.944647312164307
Epoch 200 -> MSE = 4.8798065185546875
Epoch 300 -> MSE = 4.861084461212158
Epoch 400 -> MSE = 4.84821891784668
Epoch 500 -> MSE = 4.8384623527526855
Epoch 600 -> MSE = 4.830966949462891
Epoch 700 -> MSE = 4.825173854827881
Epoch 800 -> MSE = 4.820672988891602
Epoch 900 -> MSE = 4.8171586990356445
[[ 0.5317695 ]
 [ 0.90154177]
 [ 0.15300246]
 [-0.36257118]
 [ 0.3688616 ]
 [ 0.00702283]
 [-0.04375703]
 [-0.58266324]
 [-0.55981064]]
当我试图使这段代码适应TensorFlow
2.x
时,我在
@tf.function
中看不到MSE的值。这是我的尝试和输出

尝试仅使用TensorFlow 2.x

from sklearn.preprocessing import StandardScaler
import numpy as np
from sklearn.datasets import fetch_california_housing

housing = fetch_california_housing()
m, n = housing.data.shape

housing_data_plus_bias = np.c_[np.ones((m, 1)), housing.data]

std_scaler = StandardScaler()
scaled_housing_data_plus_bias = std_scaler.fit_transform(housing_data_plus_bias)

n_epochs = 1000
learning_rate = 0.01

X = tf.constant(scaled_housing_data_plus_bias, dtype=tf.float32, name="X")
y = tf.constant(housing.target.reshape(-1, 1), dtype=tf.float32, name="y")
theta = tf.Variable(tf.compat.v1.random_uniform([n + 1, 1], -1.0, 1.0), name="theta")
y_pred = tf.matmul(X, theta, name="predictions")
error = y_pred - y
mse = tf.reduce_mean(tf.square(error), name="mse")
gradients = 2/m * tf.matmul(tf.transpose(X), error)
training_op = tf.compat.v1.assign(theta, theta - learning_rate * gradients)

init = tf.compat.v1.global_variables_initializer()

with tf.compat.v1.Session() as sess:
    sess.run(init)

    for epoch in range(n_epochs):
        if epoch % 100 == 0:
            print(f"Epoch {epoch} -> MSE = {mse.eval()}")
        sess.run(training_op)

    best_theta = theta.eval()

print(best_theta)
import numpy as np
import tensorflow as tf
from sklearn.datasets import fetch_california_housing
from sklearn.preprocessing import StandardScaler

housing = fetch_california_housing()
m, n = housing.data.shape

housing_data_plus_bias = np.c_[np.ones((m, 1)), housing.data]

std_scaler = StandardScaler()
scaled_housing_data_plus_bias = std_scaler.fit_transform(housing_data_plus_bias)

X = tf.constant(scaled_housing_data_plus_bias, dtype=tf.float32, name="X")
Y = tf.constant(housing.target.reshape(-1, 1), dtype=tf.float32, name="Y")
theta = tf.Variable(tf.random.uniform([n + 1, 1], -1.0, 1.0), name="theta")

@tf.function
def gradiente_descendente(x, y, n_epochs, learning_rate):
    for epoch in range(n_epochs):
        y_pred = tf.matmul(x, theta, name="predictions")
        error = y_pred - y
        mse = tf.reduce_mean(tf.square(error), name="mse")
        gradients = 2/m * tf.matmul(tf.transpose(x), error)
        theta.assign_add(- learning_rate * gradients, name="theta")
        if epoch % 100 == 0:
            print(f"Epoch {epoch} -> MSE = {mse}")
    return theta

result = gradiente_descendente(X, Y, n_epochs=1000, learning_rate=0.01)
print(result.numpy())
我的输出

Epoch 0 -> MSE = Tensor("mse:0", shape=(), dtype=float32)
Epoch 100 -> MSE = Tensor("mse_100:0", shape=(), dtype=float32)
Epoch 200 -> MSE = Tensor("mse_200:0", shape=(), dtype=float32)
Epoch 300 -> MSE = Tensor("mse_300:0", shape=(), dtype=float32)
Epoch 400 -> MSE = Tensor("mse_400:0", shape=(), dtype=float32)
Epoch 500 -> MSE = Tensor("mse_500:0", shape=(), dtype=float32)
Epoch 600 -> MSE = Tensor("mse_600:0", shape=(), dtype=float32)
Epoch 700 -> MSE = Tensor("mse_700:0", shape=(), dtype=float32)
Epoch 800 -> MSE = Tensor("mse_800:0", shape=(), dtype=float32)
Epoch 900 -> MSE = Tensor("mse_900:0", shape=(), dtype=float32)
[[-0.92391205]
 [ 0.95321983]
 [ 0.15357289]
 [-0.47726455]
 [ 0.47120315]
 [ 0.00655913]
 [-0.04490705]
 [-0.5299716 ]
 [-0.51395863]]
为什么这一行
mse=tf.reduce_-mean(tf.square(error),name=“mse”)
带来一个空的
shape=()
?这真的对吗?我如何从MSE张量中得到值,就像
@tf.function
中的第一个输出一样

尝试更新

我曾尝试使用
tf.print()
,但它似乎只在Jupyter笔记本或Google Colab中起作用,在终端打印中不起作用

还尝试了
tf.get_static_value(mse)
,但所有结果都是
None