1.环境安装
https://blog.youkuaiyun.com/qq_16257817/article/details/75001854
这个文章写得很好!按照它说的照做!
2.稍稍入门
现在有环境了,试试写代码
https://www.jianshu.com/p/2ea7a0632239
这个文章写得很好!但是如果你没有Python基础的话可能会有点云里雾里。不要紧!我来补充。
先照文章中说的安装jupyter
pip install jupyter
然后TensorFlow在第一步我们已经装好了,就不管了,看完概念就开始写代码。
import tensorflow as tf
v1=tf.constant([[2,3]])
v2=tf.constant([[2],[3]])
product=tf.matmul(v1,v2);
sess=tf.Session()
result=sess.run(product)
print (result)
sess.close();
随便在哪儿新建一个index.py文件,然后把上面的代码复制粘贴进去!在当前文件夹打开cmd!输入index.py。有warning,不要紧,结果已经出来了
第二段代码一样
import tensorflow as tf
num=tf.Variable(0,name="count")
new_value=tf.add(num,10);
op=tf.assign(num,new_value);
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(num))
for i in range(5):
sess.run(op)
print (sess.run(num))
建议手打一遍,理解会深刻一些
import tensorflow as tf
input1=tf.placeholder(tf.float32)
input2=tf.placeholder(tf.float32)
new_value=tf.multiply(input1,input2)
with tf.Session() as sess:
print(sess.run(new_value,feed_dict={input1:23.0,input2:11.0}))
这是第三段。
3.用tensorflow完成手写识别
https://www.jianshu.com/p/696bde1641d8
import pandas as pd
import numpy as np
import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
train=pd.read_csv("train.csv")
images=train.iloc[:,1:].values
labels_flat = train.iloc[:,0].values.ravel()
images=images.astype(np.float)
images=np.multiply(images,1.0/255.0)
print('输入数据的数量:(%g,%g)' % images.shape)
image_size=images.shape[1]
print('输入数据的纬度=》{0}'.format(image_size))
image_width=image_height=np.ceil(np.sqrt(image_size)).astype(np.uint8)
print('图片的长=>{0} \n图片的高=>{1}'.format(image_width,image_height))
x=tf.placeholder('float',shape=[None,image_size])
label_count=np.unique(labels_flat).shape[0]
print('结果的种类=》{0}'.format(label_count))
def dense_to_one_hot(labels_dense,num_classes):
num_labels=labels_dense.shape[0]
index_offset=np.arange(num_labels)*num_classes
labels_one_hot=np.zeros((num_labels,num_classes))
labels_one_hot.flat[index_offset+labels_dense.ravel()]=1
return labels_one_hot
labels=dense_to_one_hot(labels_flat,label_count).astype(np.uint8)
print('结果的数量:({0[0]},{0[1]})'.format(labels.shape))
VALIDATION_SIZE=2000
validation_images=images[:VALIDATION_SIZE]
validation_labels=labels[:VALIDATION_SIZE]
train_images=images[VALIDATION_SIZE:]
train_labels=labels[VALIDATION_SIZE:]
batch_size=100
n_batch=int(len(train_images)/batch_size)
weights=tf.Variable(tf.zeros([784,10]))
biases=tf.Variable(tf.zeros([10]))
result=tf.matmul(x,weights)+biases
prediction=tf.nn.softmax(result)
y = tf.placeholder('float', shape=[None, label_count])
loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))
train_step=tf.train.GradientDescentOptimizer(0.1).minimize(loss)
init=tf.global_variables_initializer()
correct_prediction=tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
with tf.Session() as sess:
sess.run(init)
for epoch in range(500):
for batch in range(n_batch):
batch_x=train_images[batch*batch_size:(batch+1)*batch_size]
batch_y=train_labels[batch*batch_size:(batch+1)*batch_size]
sess.run(train_step,feed_dict={x:batch_x,y:batch_y})
accuracy_n=sess.run(accuracy,feed_dict={x:validation_images,y:validation_labels})
print ("第"+str(epoch+1)+"轮,准确度为:"+str(accuracy_n))
就很棒。