1.基础框架:window10+pytorch+pycharm
2.问题:输入tensorboard指令时,显示没有该指令或不是内部指令。
3.分析:试了很多网页上的主流改错方法,但还是不行,而且在我的conda环境中,有tensorboard和TensorFlow,但还是不行。可能当下这个环境有点混乱,果断新建一个环境。
4.重建环境:
(1)
在这 我新建了一个新的环境pytorch1(图中pytorch为原环境名)
(2)进入pytorch1环境安装pytorch
conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch
(在官网找对应版本及型号安装)
(3)修改pycharm中的Python interpreter环境
(4)小例子测试:
新建python文件,复制该代码进行编译。
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
创建一个神经网络层
def add_layer(input, in_size, out_size, activation_function=None):
“”"
:param input:
神经网络层的输入
:param in_zize:
输入数据的大小
:param out_size:
输出数据的大小
:param activation_function:
神经网络激活函数,默认没有
“”"
with tf.name_scope(‘layer’):
with tf.name_scope(‘weights’):
# 定义神经网络的初始化权重
Weights = tf.Variable(tf.random_normal([in_size, out_size]))
with tf.name_scope(‘biases’):
# 定义神经网络的偏置
biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)
with tf.name_scope(‘W_mul_x_plus_b’):
# 计算w*x+b
W_mul_x_plus_b = tf.matmul(input, Weights) + biases
# 根据是否有激活函数进行处理
if activation_function is None:
output = W_mul_x_plus_b
else:
output = activation_function(W_mul_x_plus_b)
return output
创建一个具有输入层、隐藏层、输出层的三层神经网络,神经元个数分别为1,10,1
创建只有一个特征的输入数据,数据数目为300,输入层
x_data = np.linspace(-1, 1, 300)[:, np.newaxis]
创建数据中的噪声
noise = np.random.normal(0, 0.05, x_data.shape)
创建输入数据对应的输出
y_data = np.square(x_data) + 1 + noise
with tf.name_scope(‘input’):
# 定义输入数据,None是样本数目,表示多少输入数据都行,1是输入数据的特征数目
xs = tf.placeholder(tf.float32, [None, 1], name=‘x_input’)
# 定义输出数据,与xs同理
ys = tf.placeholder(tf.float32, [None, 1], name=‘y_input’)
定义一个隐藏层
hidden_layer = add_layer(xs, 1, 10, activation_function=tf.nn.relu)
定义输出层
prediction = add_layer(hidden_layer, 10, 1, activation_function=None)
求解神经网络参数
定义损失函数
with tf.name_scope(‘loss’):
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction), reduction_indices=[1]))
定义训练过程
with tf.name_scope(‘train’):
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
变量初始化
init = tf.global_variables_initializer()
定义Session
sess = tf.Session()
将网络结构图写到文件中
writer = tf.summary.FileWriter(‘logs/’, sess.graph)
执行初始化工作
sess.run(init)
绘制求解的曲线
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.scatter(x_data, y_data)
plt.ion()
plt.show()
进行训练
for i in range(1000):
# 执行训练,并传入数据
sess.run(train_step, feed_dict={xs: x_data, ys: y_data})
if i % 100 == 0:
try:
ax.lines.remove(lines[0])
except Exception:
pass
# print sess.run(loss, feed_dict = {xs: x_data, ys: y_data})
# 计算预测值
prediction_value = sess.run(prediction, feed_dict={xs: x_data})
# 绘制预测值
lines = ax.plot(x_data, prediction_value, 'r-', lw=5)
plt.pause(0.1)
关闭Session
sess.close()
备注:跑该代码时,报错:tensorflow matplotlib 两个模块没有,直接用pycharm中的自带安装也可,或者在conda中 用pip命令安装
(5)先重新打开pycharm,保证进入新建的pytorch1环境。
在pycharm的Terminal中:
出现:
而且我把这个网址输入到浏览器中,显示找不到网页。
(6)借鉴https://blog.csdn.net/qq872890060/article/details/100546719 的修正方式:
TensorBoard 1.14.0 at http://DESKTOP-AR9QBOF:6006/ (Press CTRL+C to
quit)
由于版本原因,这里不能直接在127.0.0.1:6006可视化tensorboard
解决方法:
tensorboard --logdir=文件夹路径 --host=127.0.0.1
(指定路径后就可以访问了)
果然成功: