TensorFlow 基础
https://tf.wiki/zh/basic/basic.html
如果程序报错,建议包含以下信息以帮助排除问题:
- TensorFlow 版本(请检查 TensorFlow 版本为 2.1)
import tensorflow as tf
print (tf.__version__)
- 完整的最小可运行程序代码(建议将代码在 IDE 中运行而不是交互式环境)
- 完整的报错信息(Traceback)
https://tf.wiki/zh/basic/basic.html
如果程序报错,建议包含以下信息以帮助排除问题:
import tensorflow as tf
print (tf.__version__)
这里有一个问题,教程中关于均方差损失函数的引用 4 公式前面的系数应该是
1/5 不是 1/2
大概两点:
在自动求导机制中,y 和 y_grad 的输出为什么不是 tensor 呢?
不好意思,是没及时更新造成的错误,已修改。
optimizer = tf.keras.optimizers.SGD (learning_rate=5e-4)
在 tensorflow 的程序示例中 learning_rate 和下文中 “ 在这里,我们使用了前文的方式计算了损失函数关于参数的偏导数。同时,使用 tf.keras.optimizers.SGD (learning_rate=1e-3)
声明了一个梯度下降 优化器 (Optimizer),其学习率为 1e-3。” 的 learning_rate 不一致
谢谢大佬,我以为自己的基础没打牢,看来可以自信一点点了,感谢感谢
大佬可否介绍一下 pycharm 环境的配置,申请了个 prof 版,环境不太会配置,以前都是用 colab
已修正,感谢提醒~
PyCharm 的配置应该还是比较亲民的,网上的教程也很多。我在 “IDE 配置” 里也写了一些内容。如果还有问题可以具体提问。
配置好了,但是每次运行的时候总是出现以下错误代码,是什么原因呢:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/dtypes.py:516: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,) type’.
_np_qint8 = np.dtype ([(“qint8”, np.int8, 1)])
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/dtypes.py:517: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,) type’.
_np_quint8 = np.dtype ([(“quint8”, np.uint8, 1)])
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/dtypes.py:518: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,) type’.
_np_qint16 = np.dtype ([(“qint16”, np.int16, 1)])
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/dtypes.py:519: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,) type’.
_np_quint16 = np.dtype ([(“quint16”, np.uint16, 1)])
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/dtypes.py:520: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,) type’.
_np_qint32 = np.dtype ([(“qint32”, np.int32, 1)])
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/dtypes.py:525: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,) type’.
np_resource = np.dtype ([(“resource”, np.ubyte, 1)])
/home/fish/.local/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:541: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,) type’.
_np_qint8 = np.dtype ([(“qint8”, np.int8, 1)])
/home/fish/.local/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:542: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,) type’.
_np_quint8 = np.dtype ([(“quint8”, np.uint8, 1)])
/home/fish/.local/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:543: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,) type’.
_np_qint16 = np.dtype ([(“qint16”, np.int16, 1)])
/home/fish/.local/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:544: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,) type’.
_np_quint16 = np.dtype ([(“quint16”, np.uint16, 1)])
/home/fish/.local/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:545: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,) type’.
_np_qint32 = np.dtype ([(“qint32”, np.int32, 1)])
/home/fish/.local/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:550: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / ‘(1,) type’.
np_resource = np.dtype ([(“resource”, np.ubyte, 1)])
2020-04-25 11:25:56.201023: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Could not dlopen library ‘libcuda.so.1’; dlerror: libcuda.so.1: cannot open shared object file: No such file or directory
2020-04-25 11:25:56.201044: E tensorflow/stream_executor/cuda/cuda_driver.cc:318] failed call to cuInit: UNKNOWN ERROR (303)
2020-04-25 11:25:56.201061: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (fish-desktop): /proc/driver/nvidia/version does not exist
2020-04-25 11:25:56.201256: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2020-04-25 11:25:56.235142: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 3591575000 Hz
2020-04-25 11:25:56.235842: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x5ba5910 executing computations on platform Host. Devices:
2020-04-25 11:25:56.235864: I tensorflow/compiler/xla/service/service.cc:175] StreamExecutor device
大佬你好, 我想请问一下我遇到的这个问题怎么解决:
InvalidArgumentError Traceback (most recent call last)
<ipython-input-24-dfbd31710005> in <module>
11 for e in range (num_epoch):
12 with tf.GradientTape () as tape:
---> 13 y_pred = a * X + b
14 loss = tf.reduce_sum (tf.square (y - y_pred))
15 grads = tape.gradient (loss, variables)
InvalidArgumentError: cannot compute Mul as input #1 (zero-based) was expected to be a float tensor but is a double tensor [Op:Mul] name: mul/
看上去像是数据类型的问题, 应该怎么解决呢?
import tensorflow as tf
X = tf.constant ([[1., 2.], [3., 4.]])
y = tf.constant ([[1.], [2.]])
w = tf.Variable (initial_value=[[1.], [2.]])
b = tf.Variable (initial_value=1.)
with tf.GradientTape () as tape:
L = tf.reduce_sum (tf.square (tf.matmul (X, w) + b - y))
w_grad, b_grad = tape.gradient (L, [w, b]) # 计算 L (w, b) 关于 w, b 的偏导数
print (L, w_grad, b_grad)
上面这段执行完后报错如下,请赐教如何修改,谢谢。
2020-04-26 16:31:11.896090: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
Traceback (most recent call last):
File "E:/[RemoteSync]/Shared/PycharmProjects/test/src/03.partial_derivative.py", line 8, in <module>
L = tf.reduce_sum (tf.square (tf.matmul (X, w) + b - y))
File "C:\Users\demo\AppData\Local\conda\conda\envs\tf2\lib\site-packages\tensorflow_core\python\util\dispatch.py", line 180, in wrapper
return target (*args, **kwargs)
File "C:\Users\demo\AppData\Local\conda\conda\envs\tf2\lib\site-packages\tensorflow_core\python\ops\math_ops.py", line 1595, in reduce_sum
_ReductionDims (input_tensor, axis))
File "C:\Users\demo\AppData\Local\conda\conda\envs\tf2\lib\site-packages\tensorflow_core\python\ops\math_ops.py", line 1473, in _ReductionDims
return constant_op.constant (np.arange (rank, dtype=np.int32))
File "C:\Users\demo\AppData\Local\conda\conda\envs\tf2\lib\site-packages\tensorflow_core\python\framework\constant_op.py", line 258, in constant
allow_broadcast=True)
File "C:\Users\demo\AppData\Local\conda\conda\envs\tf2\lib\site-packages\tensorflow_core\python\framework\constant_op.py", line 266, in _constant_impl
t = convert_to_eager_tensor (value, ctx, dtype)
File "C:\Users\demo\AppData\Local\conda\conda\envs\tf2\lib\site-packages\tensorflow_core\python\framework\constant_op.py", line 96, in convert_to_eager_tensor
return ops.EagerTensor (value, ctx.device_name, dtype)
ValueError: Attempt to convert a value (0) with an unsupported type (<class 'numpy.int32'>) to a Tensor.
Process finished with exit code 1
请检查一下你的X
变量的数据类型,如果是float64
的话转为float32
。
X_raw = np.array ([2013, 2014, 2015, 2016, 2017], dtype=np.float32)
y_raw = np.array ([12000, 14000, 15000, 16500, 17500], dtype=np.float32)
X = (X_raw - X_raw.min ()) / (X_raw.max () - X_raw.min ())
y = (y_raw - y_raw.min ()) / (y_raw.max () - y_raw.min ())
注意代码中的 dtype
请检查你的 TensorFlow 版本是否为 2.1 正式版
import tensorflow as tf
print (tf.__version__)
感谢回复! 上面的问题确实是数据类型的问题, 已经解决了. 可是在下一条语句又遇到了问题
<ipython-input-9-4a00192d6d84> in <module>
8 y = tf.constant (y)
9
---> 10 a = tf.Variable (initial_value=0.)
11 b = tf.Variable (initial_value=0.)
12 variables = [a, b]
InvalidArgumentError: assertion failed: [0] [Op:Assert] name: EagerVariableNameReuse
这里又出现了问题, 能麻烦大佬再帮忙解答一下吗
建议在 IDE 里新开一个.py
文件,将要运行的代码写好,然后直接运行代码。在没有上下文的情况下很难确定 ipython 环境里的报错是怎么回事。
这里面并没有真正意义上的异常报错,基本都是提示信息。
想问问这个 X 和 y 计算出的是什么,没搞懂