TensorFlow学习
线性回归问题
import matplotlib.pyplot as plt
import numpy as np
import tensorflow.compat.v1 as tf
def prepare_data():
x_data = np.linspace(-1, 1, 100)
y_data = 2 * x_data + 1.0 + np.random.randn(*x_data.shape) * 0.4
return x_data,y_data
def model(x, w, b):
return tf.multiply(x, w) + b
def train(x_data, y_data,sess,optimizer,loss_function):
global loss
for epoch in range(train_epochs):
for xs, ys in zip(x_data, y_data):
_, loss = sess.run([optimizer, loss_function], feed_dict={x: xs, y: ys})
print("第%d轮,loss:%f"%(epoch , loss))
b0temp = b.eval(session=sess)
w0temp = w.eval(session=sess)
plt.plot(x_data, w0temp * x_data + b0temp)
plt.show()
def infer(x_test):
predict = sess.run(pred, feed_dict={x: x_test})
print("预测值:%f" % predict)
target = 2 * x_test + 1.0
print("目标值:%f" % target)
tf.compat.v1.disable_eager_execution()
train_epochs = 10
learning_rate = 0.05
display_step = 10
w = tf.Variable(1.0, name="w0")
b = tf.Variable(0.0, name="b0")
x = tf.placeholder("float", name="x")
y = tf.placeholder("float", name="y")
if __name__ == '__main__':
x_data,y_data = prepare_data()
pred = model(x, w, b)
loss_function = tf.reduce_mean(tf.square(y - pred))
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss_function)
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
train(x_data,y_data,sess,optimizer,loss_function)
infer(3.21)
波士顿房价预测
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.utils import shuffle
import tensorflow.compat.v1 as tf
def prepare_data():
df = pd.read_csv("data/boston.csv", header=0)
df = df.values
df = np.array(df)
for i in range(12):
df[:, i] = (df[:, i] - df[:, i].min()) / (df[:, i].max() - df[:, i].min())
x_data = df[:, :12]
y_data = df[:, 12]
return x_data,y_data
def model(x, w, b):
return tf.multiply(x, w) + b
def train():
loss_list = []
for epoch in range(train_epochs):
loss_sum = 0.0
for xs, ys in zip(x_data, y_data):
xs = xs.reshape(1, 12)
ys = ys.reshape(1, 1)
_, loss = sess.run([optimizer, loss_function], feed_dict={x: xs, y: ys})
loss_sum = loss_sum + loss
xvalues, yvalues = shuffle(x_data, y_data)
b0temp = b.eval(session=sess)
w0temp = w.eval(session=sess)
loss_average = loss_sum / len(y_data)
loss_list.append(loss_average)
print("epoch=", epoch + 1, "loss=", loss_average, "b=", b0temp, "w=", w0temp)
plt.plot(loss_list)
plt.show()
def infer(n):
x_test = x_data[n]
x_test = x_test.reshape(1, 12)
predict = sess.run(pred, feed_dict={x: x_test})
print("预测值:%f" % predict)
target = y_data[n]
print("标签值:%f" % target)
tf.compat.v1.disable_eager_execution()
if __name__ == '__main__':
x_data,y_data = prepare_data()
with tf.name_scope("Model"):
x = tf.placeholder(tf.float32, [None, 12], name="X")
y = tf.placeholder(tf.float32, [None, 1], name="Y")
w = tf.Variable(tf.random_normal([12, 1], stddev=0.01), name="W")
b = tf.Variable(1.0, name="b")
def model(x, w, b):
return tf.matmul(x, w) + b
pred = model(x, w, b)
train_epochs = 50
learning_rate = 0.01
loss_function = tf.reduce_mean(tf.pow(y-pred, 2))
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss_function)
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
train()
infer(348)
手写体数字识别
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.utils import shuffle
import tensorflow.compat.v1 as tf
import input_data as input_data
import matplotlib.pyplot as plt
import numpy as np
def train():
for epoch in range(train_epochs):
for batch in range(total_batch):
xs, ys = mnist.train.next_batch(batch_size)
sess.run(optimizer, feed_dict={x: xs, y: ys})
loss, acc = sess.run([loss_function, accuracy],
feed_dict={x: mnist.validation.images, y: mnist.validation.labels})
if (epoch + 1) % display_step == 0:
print("Train Epoch:", '%02d' % (epoch + 1), "Loss=", "{:.9f}".format(loss), \
" Accuracy=", "{:.4f}".format(acc))
print("Train Finished!")
def accu(sess):
accu_test = sess.run(accuracy,
feed_dict={x: mnist.test.images, y: mnist.test.labels})
print("Test Accuracy:", accu_test)
accu_validation = sess.run(accuracy,
feed_dict={x: mnist.validation.images, y: mnist.validation.labels})
print("Test Accuracy:", accu_validation)
accu_train = sess.run(accuracy,
feed_dict={x: mnist.train.images, y: mnist.train.labels})
print("Test Accuracy:", accu_train)
tf.compat.v1.disable_eager_execution()
def plot_image(image):
plt.imshow(image.reshape(28, 28), cmap='binary')
plt.show()
def plot_images_labels_prediction(images,
labels,
prediction,
index,
num=10):
fig = plt.gcf()
fig.set_size_inches(10, 12)
if num > 25:
num = 25
for i in range(0, num):
ax = plt.subplot(5, 5, i + 1)
ax.imshow(np.reshape(images[index], (28, 28)),
cmap='binary')
title = "label=" + str(np.argmax(labels[index]))
if len(prediction) > 0:
title += ",predict=" + str(prediction[index])
ax.set_title(title, fontsize=10)
ax.set_xticks([]);
ax.set_yticks([])
index += 1
plt.show()
if __name__ == '__main__':
mnist = input_data.read_data_sets("data/mnist/", one_hot=True)
x = tf.placeholder(tf.float32, [None, 784], name="X")
y = tf.placeholder(tf.float32, [None, 10], name="Y")
W = tf.Variable(tf.random_normal([784, 10]),name="W")
b = tf.Variable(tf.zeros([10]),name="b")
forward=tf.matmul(x, W) + b
pred = tf.nn.softmax(forward)
train_epochs = 50
batch_size = 100
total_batch = int(mnist.train.num_examples / batch_size)
display_step = 1
learning_rate = 0.01
loss_function = tf.reduce_mean(-tf.reduce_sum(y * tf.log(pred),reduction_indices=1))
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss_function)
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
train()
accu(sess)
prediction_result = sess.run(tf.argmax(pred, 1),
feed_dict={x: mnist.test.images})
plot_images_labels_prediction(mnist.test.images,mnist.test.labels,prediction_result, 10, 25)
CIFAR数据集预测
import urllib.request
import tarfile
import tensorflow.compat.v1 as tf
import matplotlib.pyplot as plt
import os
import numpy as np
import pickle as p
from time import time
def load_CIFAR_batch(filename):
""" load single batch of cifar """
with open(filename, 'rb') as f:
data_dict = p.load(f, encoding='bytes')
images = data_dict[b'data']
labels = data_dict[b'labels']
images = images.reshape(10000, 3, 32, 32)
images = images.transpose(0, 2, 3, 1)
labels = np.array(labels)
return images, labels
def load_CIFAR_data(data_dir):
"""load CIFAR data"""
images_train = []
labels_train = []
for i in range(5):
f = os.path.join(data_dir, 'data_batch_%d' % (i + 1))
print('loading ', f)
image_batch, label_batch = load_CIFAR_batch(f)
images_train.append(image_batch)
labels_train.append(label_batch)
Xtrain = np.concatenate(images_train)
Ytrain = np.concatenate(labels_train)
del image_batch, label_batch
Xtest, Ytest = load_CIFAR_batch(os.path.join(data_dir, 'test_batch'))
print('finished loadding CIFAR-10 data')
return Xtrain, Ytrain, Xtest, Ytest
def plot_images_labels_prediction(images, labels, prediction, idx, num=10):
fig = plt.gcf()
fig.set_size_inches(12, 6)
if num > 10:
num = 10
for i in range(0, num):
ax = plt.subplot(2, 5, 1 + i)
ax.imshow(images[idx], cmap='binary')
title = str(i) + ',' + label_dict[labels[idx]]
if len(prediction) > 0:
title += '=>' + label_dict[prediction[idx]]
ax.set_title(title, fontsize=10)
idx += 1
plt.show()
def weight(shape):
return tf.Variable(tf.truncated_normal(shape, stddev=0.1), name ='W')
def bias(shape):
return tf.Variable(tf.constant(0.1, shape=shape), name = 'b')
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1,1,1,1], padding='SAME')
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
data_dir = 'data/cifar-10-batches-py/'
Xtrain, Ytrain, Xtest, Ytest = load_CIFAR_data(data_dir)
label_dict = {0: "airplane", 1: "automobile", 2: "bird", 3: "cat", 4: "deer",
5: "dog", 6: "frog", 7: "horse", 8: "ship", 9: "truck"}
plot_images_labels_prediction(Xtest, Ytest, [], 1, 10)
tf.compat.v1.disable_eager_execution()
if __name__ == '__main__':
url = 'https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz'
filepath = 'data/cifar-10-python.tar.gz'
if not os.path.isfile(filepath):
result = urllib.request.urlretrieve(url, filepath)
print('downloaded:', result)
else:
print('Data file already exists.')
if not os.path.exists("data/cifar-10-batches-py"):
tfile = tarfile.open("data/cifar-10-python.tar.gz", 'r:gz')
result = tfile.extractall('data/')
print('Extracted to ./data/cifar-10-batches-py/')
else:
print('Directory already exists.')
print('training data shape:', Xtrain.shape)
print('training labels shape:', Ytrain.shape)
print('test data shape:', Xtest.shape)
print('test labels shape:', Ytest.shape)
Xtrain[0][0][0]
Xtrain_normalize = Xtrain.astype('float32') / 255.0
Xtest_normalize = Xtest.astype('float32') / 255.0
Xtrain_normalize[0][0][0]
Ytrain[:10]
from sklearn.preprocessing import OneHotEncoder
encoder = OneHotEncoder(sparse=False)
yy = [[0], [1], [2], [3], [4], [5], [6], [7], [8], [9]]
encoder.fit(yy)
Ytrain_reshape = Ytrain.reshape(-1, 1)
Ytrain_onehot = encoder.transform(Ytrain_reshape)
Ytest_reshape = Ytest.reshape(-1, 1)
Ytest_onehot = encoder.transform(Ytest_reshape)
Ytrain_onehot.shape
Ytrain[:5]
Ytrain_onehot[:5]
tf.reset_default_graph()
with tf.name_scope('input_layer'):
x = tf.placeholder('float', shape=[None, 32, 32, 3], name="x")
with tf.name_scope('conv_1'):
W1 = weight([3, 3, 3, 32])
b1 = bias([32])
conv_1 = conv2d(x, W1) + b1
conv_1 = tf.nn.relu(conv_1)
with tf.name_scope('pool_1'):
pool_1 = max_pool_2x2(conv_1)
with tf.name_scope('conv_2'):
W2 = weight([3, 3, 32, 64])
b2 = bias([64])
conv_2 = conv2d(pool_1, W2) + b2
conv_2 = tf.nn.relu(conv_2)
with tf.name_scope('pool_2'):
pool_2 = max_pool_2x2(conv_2)
with tf.name_scope('fc'):
W3 = weight([4096, 128])
b3 = bias([128])
flat = tf.reshape(pool_2, [-1, 4096])
h = tf.nn.relu(tf.matmul(flat, W3) + b3)
h_dropout = tf.nn.dropout(h, keep_prob=0.8)
with tf.name_scope('output_layer'):
W4 = weight([128, 10])
b4 = bias([10])
pred = tf.nn.softmax(tf.matmul(h_dropout, W4) + b4)
with tf.name_scope("optimizer"):
y = tf.placeholder("float", shape=[None, 10],
name="label")
loss_function = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits
(logits=pred,
labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=0.0001) \
.minimize(loss_function)
with tf.name_scope("evaluation"):
correct_prediction = tf.equal(tf.argmax(pred, 1),
tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
train_epochs = 25
batch_size = 50
total_batch = int(len(Xtrain) / batch_size)
epoch_list = [];
accuracy_list = [];
loss_list = [];
epoch = tf.Variable(0, name='epoch', trainable=False)
startTime = time()
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
ckpt_dir = "CIFAR10_log/"
if not os.path.exists(ckpt_dir):
os.makedirs(ckpt_dir)
saver = tf.train.Saver(max_to_keep=1)
ckpt = tf.train.latest_checkpoint(ckpt_dir)
if ckpt != None:
saver.restore(sess, ckpt)
else:
print("Training from scratch.")
start = sess.run(epoch)
print("Training starts form {} epoch.".format(start + 1))
def get_train_batch(number, batch_size):
return Xtrain_normalize[number * batch_size:(number + 1) * batch_size], \
Ytrain_onehot[number * batch_size:(number + 1) * batch_size]
for ep in range(start, train_epochs):
for i in range(total_batch):
batch_x, batch_y = get_train_batch(i, batch_size)
sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})
if i % 100 == 0:
print("Step {}".format(i), "finished")
loss, acc = sess.run([loss_function, accuracy], feed_dict={x: batch_x, y: batch_y})
epoch_list.append(ep + 1)
loss_list.append(loss);
accuracy_list.append(acc)
print("Train Epoch:", '%02d' % (sess.run(epoch) + 1), \
"Loss = ", "{:.6f}".format(loss), "Accuracy = ", acc)
saver.save(sess, ckpt_dir + "CIFAR10_cnn_model.cpkt", global_step=ep + 1)
sess.run(epoch.assign(ep + 1))
duration = time() - startTime
print("Train Finished takes : ", duration)
fig = plt.gcf()
fig.set_size_inches(4, 2)
plt.plot(epoch_list, loss_list, label='loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['loss'], loc='upper right')
plt.plot(epoch_list, accuracy_list, label="accuracy")
fig = plt.gcf()
fig.set_size_inches(4, 2)
plt.ylim(0.1, 1)
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend()
plt.show()
test_total_batch = int(len(Xtest_normalize) / batch_size)
test_acc_sum = 0.0
for i in range(test_total_batch):
test_image_batch = Xtest_normalize[i * batch_size:(i + 1) * batch_size]
test_label_batch = Ytest_onehot[i * batch_size:(i + 1) * batch_size]
test_batch_acc = sess.run(accuracy, feed_dict={x: test_image_batch, y: test_label_batch})
test_acc_sum += test_batch_acc
test_acc = float(test_acc_sum / test_total_batch)
print("Test accuracy:{:.6f}".format(test_acc))
test_pred = sess.run(pred, feed_dict={x: Xtest_normalize[:10]})
prediction_result = sess.run(tf.argmax(test_pred, 1))