import warnings
warnings.filterwarnings("ignore")
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
首先我们初始化两个矩阵常量(matrix constants)m1,m2。dot_operation保存了矩阵m1和m2的相乘结果(matmul)。
m1 = tf.constant([[2, 2]])
m2 = tf.constant([[3], [3]])
dot_operation = tf.matmul(m1, m2)
dot_operation的结果没办法直接打印查看。需要创建会话(session),并运行(run)这个操作。
print(dot_operation)
>>>>result:
Tensor("MatMul:0", shape=(1, 1), dtype=int32)
tensorflow中的session是什么
Session对象是封装好的场所。操作对象(operation object)需要在这个场所里进行执行运算并得到Tensor的具体值。
## 总共两种方式
# 第一种:
sess = tf.Session()
result = sess.run(dot_operation)
print(result)
sess.close()
# 第二种:
with tf.Session() as sess:
result_ = sess.run(dot_operation)
print(result_)
>>>>result:
[[12]]
变量(Variables)
tensorflow的变量是一种由程序操纵的,可以共享和拥有持久状态的对象。变量的初始化必须在其他操作之前明确完成。最简单的方式就是添加一个全部变量初始化的操作,并且在其他操作执行之前运行该操作。
# example
init = tf.global_variables_initializer()
sess.run(init)
下面是一组关于变量的示例:
var = tf.Variable(8)
add_operation = tf.add(var, 1) #该操作返回和var相同类型的var+1变量
update_operation = tf.assign(var, add_operation) # var变量将存贮着add_operation的新值
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for _ in range(3):
sess.run(update_operation)
print(sess.run(var))
>>>>result:
9
10
11
容器(Placeholder)
placeholder是一个之后需要加载数据的容器。我们可以基于它在不需要数据的情况下创造各种各样的操作,搭建我们的计算图。我们最后把数据载入我们的计算图就通过这些placeholders。
x1 = tf.placeholder(dtype=tf.float32, shape=None)
y1 = tf.placeholder(dtype=tf.float32, shape=None)
z1 = x1 + y1
x2 = tf.placeholder(dtype=tf.float32, shape=(2,1))
y2 = tf.placeholder(dtype=tf.float32, shape=[1,2])
z2 = tf.matmul(x2, y2)
with tf.Session() as sess:
# running one operation
z1_value = sess.run(z1, feed_dict={x1: 5, y1: 6})
print(z1_value)
# 一次性跑两个操作
z1_value, z2_value = sess.run(
[z1, z2],
feed_dict = {
x1:1, y1:2,
x2:[[2],[3]], y2:[[3,3]]
})
print(z1_value)
print(z2_value)
>>>>result:
11.0
3.0
[[6. 6.]
[9. 9.]]
激活函数(Activation Function)
激活函数作用于神经元的输出。它将输出映射到新的值。我们可以看一下几种不同的激活函数的映射曲线。
x = np.linspace(-5, 5, 200)
y_relu = tf.nn.relu(x)
y_sigmoid = tf.nn.sigmoid(x)
y_tanh = tf.nn.tanh(x)
y_softplus = tf.nn.softplus(x)
sess = tf.Session()
y_relu, y_sigmoid, y_tanh, y_softplus = sess.run([y_relu, y_sigmoid, y_tanh, y_softplus])
plt.figure(1, figsize=(8, 6))
plt.subplot(221)
plt.plot(x, y_relu, c='red', label='relu')
plt.ylim((-1, 5))
plt.legend(loc='best')
plt.subplot(222)
plt.plot(x, y_sigmoid, c='red', label='sigmoid')
plt.ylim((-0.2, 1.2))
plt.legend(loc='best')
plt.subplot(223)
plt.plot(x, y_tanh, c='red', label='tanh')
plt.ylim((-1.2, 1.2))
plt.legend(loc='best')
plt.subplot(224)
plt.plot(x, y_softplus, c='red', label='softplus')
plt.ylim((-0.2, 6))
plt.legend(loc='best')
plt.show()
sess.close()
Tensorflow网络
回归(Regression)
接下来我们生成一些数据作为数据集用于训练神经网络(NN)用于预测我们的数据。
tf.set_random_seed(1)
np.random.seed(1)
# data initialization
# linspace creates a list with continous intervals of range -1 to 1
# newaxis increases the dimension i.e. changes (1,100) to (100,1)
x = np.linspace(-1,1,100)[:, np.newaxis]
noise = np.random.normal(0,0.1,size=x.shape)
y = np.power(x,2) + noise
# plot the data
plt.scatter(x,y)
plt.show()
![](https://img.haomeiwen.com/i14387082/d8a439f351febf58.png)
tf_x = tf.placeholder(tf.float32, x.shape)
tf_y = tf.placeholder(tf.float32, y.shape)
# hidden layer
l1 = tf.layers.dense(tf_x,10,tf.nn.relu)
# output layer
output = tf.layers.dense(l1, 1)
# compute cost
loss = tf.losses.mean_squared_error(tf_y, output)
optimizer = tf.train.GradientDescentOptimizer(learning_rate = 0.5)
train_op = optimizer.minimize(loss)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
plt.ion()
for step in range(100):
_, l, pred = sess.run([train_op, loss, output], {tf_x: x, tf_y: y})
if step%5 == 0:
plt.cla()
plt.scatter(x,y)
plt.plot(x, pred, 'r-', lw=5)
plt.text(0.5, 0, 'loss=%.4f' % l, fontdict = {'size': 20, 'color':'red'})
plt.pause(0.1)
plt.ioff()
plt.show()
可以看到图中的红色线越来越拟合我们创建的伪数据集。
![](https://img.haomeiwen.com/i14387082/a7cda64503e8409c.png)
我们可以通过给layer加名字的方式来访问它的权重:
l1 = tf.layers.dense(tf_x,10,tf.nn.relu, name="layer1")
sess = tf.Session()
with tf.variable_scope("hidden", reuse=True):
w = tf.get_variable('kernel')
print(sess.run(w))
sess.close()
分类
创建虚拟数据
n_data = np.ones((100,2))
# numpy.random.normal 从标准正太分布中抓取样本数据
x0 = np.random.normal(2*n_data,1) # class0 x shape =(100,2)
y0 = np.zeros(100) # class0 y shape =(100,1)
x1 = np.random.normal(-2*n_data, 1) # class1 x shape =(100,2)
y1 = np.ones(100) # class1 y shape =(100,1)
# vertical stacking
x = np.vstack((x0, x1)) # shape (200,2) + some noise
# horizontal stacking
y = np.hstack((y0, y1)) # shape (200, )
plt.scatter(x[:, 0], x[:, 1], c=y, s=100, lw=0, cmap='RdYlGn')
plt.show()
![](https://img.haomeiwen.com/i14387082/b3d5f6c375dfef7d.png)
# input x and y
tf_x = tf.placeholder(tf.float32, x.shape)
tf_y = tf.placeholder(tf.int32, y.shape)
l1 = tf.layers.dense(tf_x, 10, tf.nn.relu) # hidden layer
output = tf.layers.dense(l1, 2) # output layer
loss = tf.losses.sparse_softmax_cross_entropy(labels=tf_y, logits=output) # cost function
# return (acc, update_op), and create 2 local variable
accuracy = tf.metrics.accuracy(labels=tf.squeeze(tf_y), predictions=tf.argmax(output, axis=1),)[1]
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.05)
train_op = optimizer.minimize(loss)
sess = tf.Session()
init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
sess.run(init_op)
plt.ion()
for step in range(100):
_, acc, pred = sess.run([train_op, accuracy, output], {tf_x: x, tf_y: y})
if step%2 == 0:
plt.cla()
plt.scatter(x[:, 0], x[:, 1], c=pred.argmax(1), s=100, lw=0, cmap='RdYlGn')
plt.text(1.5, -4, 'Accuracy=%.2f' % acc, fontdict={'size': 20, 'color': 'red'})
plt.pause(0.1)
plt.ioff()
plt.show()
![](https://img.haomeiwen.com/i14387082/4901d48ab741a09e.png)
网友评论