探索tensorflow
import math
import numpy as np
import h5py
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.python.framework import ops
%matplotlib inline
np.random.seed(1)
#加载数据
def load_dataset():
train_dataset = h5py.File('datasets/train_signs.h5', "r")
train_set_x_orig = np.array(train_dataset["train_set_x"][:])
train_set_y_orig = np.array(train_dataset["train_set_y"][:])
test_dataset = h5py.File('datasets/test_signs.h5', "r")
test_set_x_orig = np.array(test_dataset["test_set_x"][:])
test_set_y_orig = np.array(test_dataset["test_set_y"][:])
classes = np.array(test_dataset["list_classes"][:])
train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))
test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))
return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes
#查看一下数据
train_x, train_y, test_x, test_y, classes = load_dataset()
print('train_x.shape :',train_x.shape)
print('train_y.shape :',train_y.shape)
print('test_x.shape :',test_x.shape)
print('test_y.shape :',test_y.shape)
print('classes.shape :',classes.shape)
train_x.shape : (1080, 64, 64, 3)
train_y.shape : (1, 1080)
test_x.shape : (120, 64, 64, 3)
test_y.shape : (1, 120)
classes.shape : (6,)
#样本随机分组
def random_mini_batches(X, Y, mini_batch_size = 64, seed = 0):
#随机种子
np.random.seed(seed)
m = X.shape[1]
#把一个大样本拆分成多个小样本
mini_batches = []
#随机化索引
permutation = list(np.random.permutation(m))
#x与y成对洗牌
shuffled_X = X[:, permutation]
shuffled_Y = Y[:, permutation].reshape((1,m))
#需要分成多少组
num_complete_minibatches = math.floor(m/mini_batch_size)
for k in range(0, num_complete_minibatches):
#每组的样本
mini_batch_X = shuffled_X[:, k*mini_batch_size : (k+1)*mini_batch_size]
mini_batch_Y = shuffled_Y[:, k*mini_batch_size : (k+1)*mini_batch_size]
#添加到mini_batches中
mini_batch = (mini_batch_X, mini_batch_Y)
mini_batches.append(mini_batch)
#没法整除的情况下需要添加最后一组
if m % mini_batch_size != 0:
#添加最后一组
mini_batch_X = shuffled_X[:, num_complete_minibatches*mini_batch_size : m]
mini_batch_Y = shuffled_Y[:, num_complete_minibatches*mini_batch_size : m]
mini_batch = (mini_batch_X, mini_batch_Y)
mini_batches.append(mini_batch)
#返回多个小样本
return mini_batches
def convert_to_one_hot(Y, C):
Y = np.eye(C)[Y.reshape(-1)].T
return Y
def predict(X, parameters):
W1 = tf.convert_to_tensor(parameters["W1"])
b1 = tf.convert_to_tensor(parameters["b1"])
W2 = tf.convert_to_tensor(parameters["W2"])
b2 = tf.convert_to_tensor(parameters["b2"])
W3 = tf.convert_to_tensor(parameters["W3"])
b3 = tf.convert_to_tensor(parameters["b3"])
params = {"W1": W1,
"b1": b1,
"W2": W2,
"b2": b2,
"W3": W3,
"b3": b3}
x = tf.placeholder("float", [12288, 1])
z3 = forward_propagation_for_predict(x, params)
p = tf.argmax(z3)
sess = tf.Session()
prediction = sess.run(p, feed_dict = {x: X})
return prediction