gpt4 book ai didi

python - TensorFlow:Python 中的所有基本操作都在 Tensorflow 中被覆盖吗

转载 作者:行者123 更新时间:2023-11-30 09:17:58 25 4
gpt4 key购买 nike

我是 tensorflow 新手,尝试使用基本的 python 运算符编写损失函数(平方损失),但它不起作用。谁能告诉我我哪里错了。提前致谢

n = x_data.shape[0]
L = (Y_pred-y)**2
loss = (1/n)*tf.reduce_sum(L)

当我运行相应的 session 时,我得到loss=0.0

_ ,_m, _c, _l = session.run([optimizer,m,c,loss], feed_dict={x: x_data, y: y_data})

y 是占位符

loss = tf.reduce_mean(tf.squared_difference(Y_pred,y))

这很好用吗?

完整代码:

import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

#downloading dataset
!wget -nv -O /resources/data/PierceCricketData.csv https://ibm.box.com/shared/static/reyjo1hk43m2x79nreywwfwcdd5yi8zu.csv


df = pd.read_csv("/resources/data/PierceCricketData.csv")
df.head()

%matplotlib inline

x_data, y_data = (df["Chirps"].values,df["Temp"].values)

plt.plot(x_data, y_data, 'ro')
# label the axis
plt.xlabel("# Chirps per 15 sec")
plt.ylabel("Temp in Farenhiet")


x = tf.placeholder(tf.float32, shape=x_data.shape)
y = tf.placeholder(tf.float32, shape=y_data.shape)
m = tf.Variable(3.0, name='m')
c = tf.Variable(2.0, name='c')

Y_pred = m*x+c



n = x_data.shape[0]
L = (Y_pred*nf-y*nf)**2
loss = (1/n)*tf.reduce_sum(L)

# loss = tf.reduce_mean(tf.squared_difference(Y_pred,y))


optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(loss)

session = tf.Session()
session.run(tf.global_variables_initializer())

convergenceTolerance = 0.0001
previous_m = np.inf
previous_c = np.inf

steps = {}
steps['m'] = []
steps['c'] = []

losses=[]

for k in range(100000):
_ ,_m, _c, _l = session.run([optimizer,m,c,loss], feed_dict={x: x_data, y: y_data})



steps['m'].append(_m)
steps['c'].append(_c)
losses.append(_l)
if (np.abs(previous_m - _m) <= convergenceTolerance) or (np.abs(previous_c - _c) <= convergenceTolerance):

print "Finished by Convergence Criterion"
print k
print _l
break
previous_m = _m,
previous_c = _c,
print(losses)

我得到的输出是[0.0, 0.0]为什么?

最佳答案

这是mean_squared_error的官方TensorFlow实现:

from tensorflow.python.framework import ops, math_ops
@tf_export("losses.mean_squared_error")
def mean_squared_error(labels, predictions, weights=1.0, scope=None,
loss_collection=ops.GraphKeys.LOSSES,
reduction=Reduction.SUM_BY_NONZERO_WEIGHTS):
if labels is None:
raise ValueError("labels must not be None.")
if predictions is None:
raise ValueError("predictions must not be None.")
with ops.name_scope(scope, "mean_squared_error",(predictions, labels, weights)) as scope:
predictions = math_ops.to_float(predictions)
labels = math_ops.to_float(labels)
predictions.get_shape().assert_is_compatible_with(labels.get_shape())
losses = math_ops.squared_difference(predictions, labels)
return compute_weighted_loss(losses, weights, scope, loss_collection, reduction=reduction)

正如您在源代码中看到的,您应该确保张量具有相同的数据类型。希望这能回答您的问题。

关于python - TensorFlow:Python 中的所有基本操作都在 Tensorflow 中被覆盖吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50197560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com