import tensorflow as tf
import numpy as np
#创造data
x_data=np.random.rand(100).astype(np.float32)
#因为tf中大部分的数据是float32
y_data=x_data*0.1+0.3
#创造tf结构#######
Weights=tf.Variable(tf.random_uniform([1],-1.,1.))
#random_uniform(
shape,
minval=0,
maxval=None,
dtype=tf.float32,
seed=None,
name=None
#) https://www.w3cschool.cn/tensorflow_python/tensorflow_python-rnix2gv7.html
biases=tf.Variable(tf.zeros([1]))
y=Weights*x_data+biases
loss=tf.reduce_mean(tf.square(y-y_data))
#优化器,有很多种优化器
optimizer=tf.train.GradientDescentOptimizer(0.1)
#优化谁?loss
train=optimizer.minimize(loss)
#虽然建立了上面的各种变量,但是没有初始化,
init=tf.initialize_all_variables()
#创造tf结构end######
#tf就是先搭建结构然后初始化让他活动起来
sess=tf.Session()
sess.run(init)
#训练
for step in range(500):
sess.run(train)
if step%50==0:
print(step,sess.run(Weights),sess.run(biases),sess.run(loss))
tensorflow的数学运算
https://blog.csdn.net/zywvvd/article/details/78593618
#session
import tensorflow as tf
matrix1=tf.constant([[3,4]])
matrix2=tf.constant([[5],
[6]])
product=tf.matmul(matrix1,matrix2)
#方法1
#sess=tf.Session()
#print(sess.run(product))#run一次tf才会执行一下结构
#sess.close()
#方法2
with tf.Session() as sess:
print(sess.run(product))
print(sess.run(product))
#变量
ne=tf.Variable(0,name='one')
two=tf.constant(1)
new=tf.add(one,two)
two=tf.assign(one,new)#赋值
#一般有了变量的设置都需进行下面一步
init=tf.global_variables_initializer()
sess=tf.Session()
sess.run(init)
for i in range(3):
sess.run(two)#一般执行所有运算的最后一步就可以了
print(sess.run(one))#想要看哪个run一下就好了
#placeholder
#Tensorflow 如果想要从外部传入data, 那就需要用到 tf.placeholder(),
# 然后以这种形式传输数据 sess.run(***, feed_dict={input: **}).
input1=tf.placeholder(tf.float32)
input2=tf.placeholder(tf.float32)
output=tf.multiply(input1,input2)
sess=tf.Session()
print(sess.run(output,feed_dict={input1:3,input2:4}))
#mnist数据集
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np
import matplotlib.pyplot as plt
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
def add_layer(inputs, in_size, out_size, activation_function=None,):
# add one more layer and return the output of this layer
Weights = tf.Variable(tf.random_normal([in_size, out_size]))
biases = tf.Variable(tf.zeros([1, out_size]) + 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
#定义placeholder for inputs to network
xs=tf.placeholder(tf.float32,[None,784])#28*28
ys=tf.placeholder(tf.float32,[None,10])
#add output layer
prediction=add_layer(xs,784,10,tf.nn.softmax)
#loss
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction),
reduction_indices=[1])) # loss
import numpy as np
import matplotlib.pyplot as plt
#x是一个横向量,W的行数表示输入神经元个数,列数表示输出神经元个数
#两层神经元之间的变换就是通过一个wx+b的矩阵乘法加法变化
def add_layer(inputs,in_size,out_size,n_layer,afunc=None):
layer_name='layer%s'%n_layer
with tf.name_scope(layer_name):
with tf.name_scope('weight'):
Weights=tf.Variable(tf.random_normal([in_size,out_size]))
tf.summary.histogram(layer_name+'/weight',Weights)#观察想查看变化的变量
with tf.name_scope('bias'):
biases=tf.Variable(tf.zeros([1,out_size])+0.1)
tf.summary.histogram(layer_name+'/biases',biases)
Wxb=tf.matmul(inputs,Weights)+biases
if afunc is None:
outputs=Wxb
else:
outputs=afunc(Wxb)
tf.summary.histogram(layer_name+'/outputs',outputs)
return outputs
#构造输入训练的数据
x_data=np.linspace(-1,1,300)[:,np.newaxis]#shape(300,1)
#[:,np.newaxis]多增加一个维度,从[2]->[[2]]
y_data=np.square(x_data)-0.5
#增加噪点
noise=np.random.normal(0,0.05,x_data.shape)
y_data=y_data+noise
#高斯分布
#==============================================================================
# numpy.random.normal(loc=0.0, scale=1.0, size=None)
# loc:float
# 此概率分布的均值(对应着整个分布的中心centre)
# scale:float
# 此概率分布的标准差(对应于分布的宽度,scale越大越矮胖,scale越小,越瘦高)
# size:int or tuple of ints
# 输出的shape,默认为None,只输出一个值
#==============================================================================
#输入层是定死的一般输入的数据多少个属性就有多少个输入的神经元
#神经元的个数就是属性个数
with tf.name_scope('inputs'):
xs=tf.placeholder(tf.float32,[None,1],name='x_input')
ys=tf.placeholder(tf.float32,[None,1],name='y_input')
L1=add_layer(xs,1,10,n_layer=1,afunc=tf.nn.relu)
pre=add_layer(L1,10,1,n_layer=2,afunc=None)
with tf.name_scope('loss'):
loss=tf.reduce_mean(tf.reduce_sum(tf.square(pre-ys),reduction_indices=[1]))
tf.summary.scalar('loss', loss)
with tf.name_scope('train'):
train_step=tf.train.GradientDescentOptimizer(0.1).minimize(loss)
init=tf.global_variables_initializer()
sess=tf.Session()
#合并所有的summary
merged = tf.summary.merge_all()
#下面一步要在sess定义之后写
writer= tf.summary.FileWriter(r"C:\Users\Lenovo\Desktop\nn\g",sess.graph)#就是把框架graph放在前面的文件夹中
sess.run(init)
fig=plt.figure()#新建一个画布https://blog.csdn.net/henni_719/article/details/77479912
ax=fig.add_subplot(1,1,1)#一行一列的第一个左右上下https://blog.csdn.net/s201402023/article/details/51536687
ax.scatter(x_data,y_data)#画散点图https://blog.csdn.net/m0_37393514/article/details/81298503
#plt.ion()
for step in range(1000):
sess.run(train_step,feed_dict={xs:x_data,ys:y_data})
if step%50==0:
result=sess.run(merged,
feed_dict={xs:x_data,ys:y_data})
writer.add_summary(result,step)
#==============================================================================
# try:
# #这两种方法都可以删除前一条画的线
# ax.lines.pop(0)
# # ax.lines.remove(line[0])
# except Exception:
# pass
# #print(sess.run(loss,feed_dict={xs:x_data,ys:y_data}))
# y_p=sess.run(pre,feed_dict={xs:x_data,ys:y_data})
# line=ax.plot(x_data,y_p,'r-',lw=5)
# plt.pause(0.1)
#==============================================================================
#plt.ioff()
#将一维数组转化为onehot编码
import tensorflow as tf
labels = [1, 3, 4, 8, 7, 5, 2, 9, 0, 8, 7]
labels_expand = tf.expand_dims(labels, axis=1) # 这样label_expand为[11, 1]的数据
index_expand = tf.expand_dims(tf.range(len(labels)), axis=1) # 与label_expand中的元素一一对应
concat_result = tf.concat(values=[index_expand, labels_expand], axis=1) # 将上述两组数据组合在一起
one_hot = tf.sparse_to_dense(sparse_indices=concat_result,
output_shape=tf.zeros([len(labels), 10]).shape,
sparse_values=1.0, default_value=0.0)
session = tf.Session()
print('one_hot_of_labels:{}'.format(session.run(one_hot)))
#模型参数的保存和load但是有问题
可参考学习
https://blog.csdn.net/liuxiao214/article/details/79048136
import tensorflow as tf
import numpy as np
#####保存
#W=tf.Variable([[1,2,3],
# [3,4,5]],dtype=tf.float32,name="weights")
#b=tf.Variable([[1,2,3]],dtype=tf.float32,name='biases')
#
#init=tf.global_variables_initializer()
#saver=tf.train.Saver()
#with tf.Session() as sess:
# sess.run(init)
# save_path=saver.save(sess,r'C:\Users\Lenovo\Desktop\nn\g\s1.ckpt')
# print("save to",save_path)
#restore variable
#redefine the same shape and same type for your variable
##先定义一个空的变量框架,用于存将load的变量
W=tf.Variable(np.arange(6).reshape((2,3)),dtype=tf.float32
,name='weights')
b=tf.Variable(np.arange(3).reshape((1,3)),dtype=tf.float32
,name='biases')
#不用定义initial
#saver=tf.train.Saver()
#sess=tf.Session()
#saver.restore(sess,r'C:\Users\Lenovo\Desktop\nn\g\s1.ckpt')
#print('weights',sess.run(W))
#print('biases',sess.run(b))
saver = tf.train.Saver()
with tf.Session() as sess:
saver.restore(sess,r'C:\Users\Lenovo\Desktop\nn\g\s1.ckpt')
print("weights:", sess.run(W))
print("biases:", sess.run(b))