2019.12.06
一、tensorflow
1.使用tensorflow建造神經網絡
1.定義一個函數,可以增加一層隱藏層,並返回該隱藏層的輸出值。
2.構造一些數據,用於處理。
3.定義placeholder用於神經網絡的輸入
4.用步驟1定義好的函數構造各個hidden layer和output layer
5.初始化所有參數initialize_all_variables()
6.構建session,並run。
7.使用for語句循環訓練n次。
from __future__ import print_function
import tensorflow as tf
import numpy as np
def add_layer(inputs, in_size, out_size, activation_function=None):
# 增加一層隱藏層,返回隱藏層的輸出值
Weights = tf.Variable(tf.random_normal([in_size, out_size]))
biases = tf.Variable(tf.zeros([1, out_size])+0.1) # 加0.1使得初始化片偏差不爲零
Wx_plus_b = tf.matmul(inputs, Weights) + biases
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b)
return outputs
# Make up some real data
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) - 0.5 + noise
# define placeholder for inputs for network
xs = tf.placeholder(tf.float32, [None, 1])
ys = tf.placeholder(tf.float32, [None, 1])
# add hidden layer
l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu)
# add output layer
prediction = add_layer(l1, 10, 1, activation_function=None)
# define the error between prediction and real data
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
# initialize_all_variables()
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
# 訓練2000次
for i in range(2000):
sess.run(train_step, feed_dict={xs: x_data, ys: y_data})
if (i <= 100) and i % 10 == 0:
print('100以內')
print(sess.run(loss, feed_dict={xs: x_data, ys: y_data}))
if (i > 100) and (i % 50) == 0:
print(sess.run(loss, feed_dict={xs: x_data, ys: y_data}))
二、Win10家庭版安裝OpenAI Gym和Universe
1.由於openai不支持Windows系統,因此需要在Windows系統上安裝docker,主要參考這篇文章。
2.首先用pip install gym之後,會出現報錯。報錯和解決辦法在這篇文章內。
3.py3 安裝 box2d-py時出現的問題,解決方法在這篇文章內。
4.今天安裝的Universe沒有成果,似乎Universe真的只支持Linux和mac os。明天考慮要不要再裝個虛擬機或者先不裝直接往下學。
三、用VMware Workstation安裝Ubuntu操作系統
1.還是決定直接裝個虛擬機。
參考文章在此,一篇足矣,講的不錯
2.在安裝Ubuntu的時候,下載100多個文件可能會很慢,如果有VPN一定要用VPN,下載速度會快很多很多。
3.若按照的Ubuntu界面特別小,可通過此方法解決。