コード例 #1
0
def __init__():
    # Loading the dataset
    X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = utils.load_dataset(
        'dataset/signs/test_signs.h5', 'dataset/signs/train_signs.h5')

    # Flatten the image dataset, then normalize it by dividing by 255. On top of that, you will convert
    # each label to a one-hot vector as shown in Figure 1. Run the cell below to do so.

    # Flatten the training and test images
    X_train_flatten = X_train_orig.reshape(X_train_orig.shape[0], -1).T
    X_test_flatten = X_test_orig.reshape(X_test_orig.shape[0], -1).T
    # Normalize image vectors
    X_train = X_train_flatten / 255.
    X_test = X_test_flatten / 255.
    # Convert training and test labels to one hot matrices
    Y_train = utils.convert_to_one_hot(Y_train_orig, 6)
    Y_test = utils.convert_to_one_hot(Y_test_orig, 6)

    print("number of training examples = " + str(X_train.shape[1]))
    print("number of test examples = " + str(X_test.shape[1]))
    print("X_train shape: " + str(X_train.shape))
    print("Y_train shape: " + str(Y_train.shape))
    print("X_test shape: " + str(X_test.shape))
    print("Y_test shape: " + str(Y_test.shape))

    X, Y = utils.create_placeholders(12288, 6)
    print("X = " + str(X))
    print("Y = " + str(Y))

    parameters = model(X_train, Y_train, X_test, Y_test)
    print('PARAMETERS', parameters)
コード例 #2
0
def main():
    print("result = " + str(linear_function()))

    X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = load_dataset()

    # index = 36
    # plt.imshow(X_train_orig[index])
    # print("y = " + str(np.squeeze(Y_train_orig[:, index])))
    # plt.show()

    # model-part
    # Flatten the training and test images
    X_train_flatten = X_train_orig.reshape(X_train_orig.shape[0], -1).T
    X_test_flatten = X_test_orig.reshape(X_test_orig.shape[0], -1).T
    # Normalize image vectors
    X_train = X_train_flatten / 255.
    X_test = X_test_flatten / 255.

    # Convert training and test labels to one hot matrices
    Y_train = convert_to_one_hot(Y_train_orig, 6)
    Y_test = convert_to_one_hot(Y_test_orig, 6)
    #
    learned_parameters = model(X_train, Y_train, X_test, Y_test, num_epochs=1000)

    return learned_parameters
コード例 #3
0
def main():

    X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = load_dataset(
    )

    # Flatten the training and test images
    X_train_flatten = X_train_orig.reshape(X_train_orig.shape[0], -1).T
    X_test_flatten = X_test_orig.reshape(X_test_orig.shape[0], -1).T
    # Normalize image vectors
    X_train = X_train_flatten / 255.
    X_test = X_test_flatten / 255.

    # Convert training and test labels to one hot matrices
    Y_train = convert_to_one_hot(Y_train_orig, 6)
    Y_test = convert_to_one_hot(Y_test_orig, 6)
    #
    start_time = time.time()
    learned_parameters = model(X_train,
                               Y_train,
                               X_test,
                               Y_test,
                               num_epochs=600)

    print("--- %s seconds ---" % (time.time() - start_time))

    return learned_parameters
コード例 #4
0
def get_dataset():
    X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = load_dataset()
    index = 0
    #plt.imshow(X_train_orig[index])
    #print ("y = " + str(np.squeeze(Y_train_orig[:, index])))
    # 扁平化
    X_train_flatten = X_train_orig.reshape(X_train_orig.shape[0], -1).T
    X_test_flatten = X_test_orig.reshape(X_test_orig.shape[0], -1).T
    # 简单的归一化
    X_train = X_train_flatten / 255.
    X_test = X_test_flatten / 255.
    # one hot编码
    Y_train = convert_to_one_hot(Y_train_orig, 6)
    Y_test = convert_to_one_hot(Y_test_orig, 6)
    
    return X_train, X_test, Y_train, Y_test
コード例 #5
0
def load():
    X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = load_dataset(
    )
    # Flatten the training and test images
    X_train_flatten = X_train_orig.reshape(X_train_orig.shape[0], -1).T
    X_test_flatten = X_test_orig.reshape(X_test_orig.shape[0], -1).T
    # Normalize image vectors
    X_train = X_train_flatten / 255.
    X_test = X_test_flatten / 255.
    # Convert training and test labels to one hot matrices
    Y_train = convert_to_one_hot(Y_train_orig, 6)
    Y_test = convert_to_one_hot(Y_test_orig, 6)

    print("number of training examples = " + str(X_train.shape[1]))
    print("number of test examples = " + str(X_test.shape[1]))
    print("X_train shape: " + str(X_train.shape))
    print("Y_train shape: " + str(Y_train.shape))
    print("X_test shape: " + str(X_test.shape))
    print("Y_test shape: " + str(Y_test.shape))
    return X_train, Y_train, X_test, Y_test
コード例 #6
0
ファイル: DNN1.py プロジェクト: wmylxmj/Deep-Learning
                accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
                print("Accuracy:", 100 * accuracy.eval({
                    X: X_test,
                    Y: Y_test
                }), "%")
                pass
            else:
                print('No checkpoints found!')
        pass

    pass


tf.reset_default_graph()
# Loading the dataset
X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = load_dataset()

# Example of a picture
index = 0
plt.imshow(X_train_orig[index])
plt.show()
print("y = " + str(np.squeeze(Y_train_orig[:, index])))

# Flatten the training and test images
X_train_flatten = X_train_orig.reshape(X_train_orig.shape[0], -1).T
X_test_flatten = X_test_orig.reshape(X_test_orig.shape[0], -1).T
# Normalize image vectors
X_train = X_train_flatten / 255.
X_test = X_test_flatten / 255.
# Convert training and test labels to one hot matrices
Y_train = convert_to_one_hot(Y_train_orig, 6)
コード例 #7
0
    sess = tf.Session()
    
    # Run the session to compute 'ones' (approx. 1 line)
    ones = sess.run(ones)
    
    # Close the session (approx. 1 line). See method 1 above.
    sess.close()
    
    ### END CODE HERE ###
    return ones


# # 2 - Building your first neural network in tensorflow

# Loading the dataset
X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = load_dataset()

def create_placeholders(n_x, n_y):
    """
    Creates the placeholders for the tensorflow session.
    
    Arguments:
    n_x -- scalar, size of an image vector (num_px * num_px = 64 * 64 * 3 = 12288)
    n_y -- scalar, number of classes (from 0 to 5, so -> 6)
    
    Returns:
    X -- placeholder for the data input, of shape [n_x, None] and dtype "float"
    Y -- placeholder for the input labels, of shape [n_y, None] and dtype "float"
    
    Tips:
    - You will use None because it let's us be flexible on the number of examples you will for the placeholders.
コード例 #8
0

def ones(shape):
    """
    print(ones([3, 3]))
    :param shape:
    """
    ones = tf.ones(shape)
    sess = tf.Session()
    result = sess.run(ones)
    sess.close()
    return result


# (1080,64,64,3)  (1,1080)    (120,64,64,3)  (1,120)   [1 2 3 4 5]
X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = tf_utils.load_dataset(
)

# 归一化数据
X_train = X_train_orig / 255
X_test = X_test_orig / 255

# reshape
X_train = X_train.reshape(X_train.shape[0], -1).T
X_test = X_test.reshape(X_test.shape[0], -1).T

# 转换为one-hot矩阵
Y_train = tf_utils.convert_to_one_hot(
    Y_train_orig, 6)  # 根据label进行独热编码 行=深度=6=[012345], 列=训练样本数 =1080
Y_test = tf_utils.convert_to_one_hot(
    Y_test_orig, 6)  # 根据label进行独热编码 行=深度=6=[012345], 列=训练样本数 =120
コード例 #9
0
def main():
    np.random.seed(1)

    y_hat = tf.constant(36, name='y_hat')  # Define y_hat constant. Set to 36.
    y = tf.constant(39, name='y')  # Define y. Set to 39

    loss = tf.Variable((y - y_hat) ** 2, name='loss')  # Create a variable for the loss

    init = tf.global_variables_initializer()  # When init is run later (session.run(init)),
    # the loss variable will be initialized and ready to be computed
    with tf.Session() as session:  # Create a session and print the output
        session.run(init)  # Initializes the variables
        print(session.run(loss))  # Prints the loss

    a = tf.constant(2)
    b = tf.constant(10)
    c = tf.multiply(a, b)
    print(c)

    sess = tf.Session()
    print(sess.run(c))

    # Change the value of x in the feed_dict
    x = tf.placeholder(tf.int64, name='x')
    print(sess.run(2 * x, feed_dict={x: 3}))
    sess.close()


    """ TEST 1 """
    print("result = " + str(linear_function()))

    """ TEST 2 """
    print("sigmoid(0) = " + str(sigmoid(0)))
    print("sigmoid(12) = " + str(sigmoid(12)))

    """ TEST 3 """
    logits = sigmoid(np.array([0.2, 0.4, 0.7, 0.9]))
    cost = cost_func(logits, np.array([0, 0, 1, 1]))
    print("cost = " + str(cost))

    """ TEST 4 """
    labels = np.array([1, 2, 3, 0, 2, 1])
    one_hot = one_hot_matrix(labels, C=4)
    print("one_hot = " + str(one_hot))

    """ TEST 5 """
    print("ones = " + str(ones([3])))


    # Loading the dataset
    X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = load_dataset()

    # Change the index below and run the cell to visualize some examples in the dataset.
    # Example of a picture
    index = 99
    #plt.imshow(X_train_orig[index])
    print("y = " + str(np.squeeze(Y_train_orig[:, index])))

    # Flatten the training and test images
    X_train_flatten = X_train_orig.reshape(X_train_orig.shape[0], -1).T
    X_test_flatten = X_test_orig.reshape(X_test_orig.shape[0], -1).T
    # Normalize image vectors
    X_train = X_train_flatten / 255.
    X_test = X_test_flatten / 255.
    # Convert training and test labels to one hot matrices
    Y_train = convert_to_one_hot(Y_train_orig, 6)
    Y_test = convert_to_one_hot(Y_test_orig, 6)

    print("number of training examples = " + str(X_train.shape[1]))
    print("number of test examples = " + str(X_test.shape[1]))
    print("X_train shape: " + str(X_train.shape))
    print("Y_train shape: " + str(Y_train.shape))
    print("X_test shape: " + str(X_test.shape))
    print("Y_test shape: " + str(Y_test.shape))
    plt.show()

    """ TEST 6 """
    X, Y = create_placeholders(12288, 6)
    print("X = " + str(X))
    print("Y = " + str(Y))

    """ TEST 7 """
    tf.reset_default_graph()
    with tf.Session() as sess:
        parameters = initialize_parameters()
        print("W1 = " + str(parameters["W1"]))
        print("b1 = " + str(parameters["b1"]))
        print("W2 = " + str(parameters["W2"]))
        print("b2 = " + str(parameters["b2"]))


    """ TEST 8 """
    tf.reset_default_graph()

    with tf.Session() as sess:
        X, Y = create_placeholders(12288, 6)
        parameters = initialize_parameters()
        Z3 = forward_propagation(X, parameters)
        print("Z3 = " + str(Z3))

    """ TEST 9 """
    tf.reset_default_graph()

    with tf.Session() as sess:
        X, Y = create_placeholders(12288, 6)
        parameters = initialize_parameters()
        Z3 = forward_propagation(X, parameters)
        cost = compute_cost(Z3, Y)
        print("cost = " + str(cost))

    """ TEST 10 """
    parameters = model(X_train, Y_train, X_test, Y_test)


    """ TEST 11 """
    ## START CODE HERE ## (PUT YOUR IMAGE NAME)
    my_image = "thumbs_up.jpg"
    ## END CODE HERE ##

    # We preprocess your image to fit your algorithm.
    fname = "images/" + my_image
    image = np.array(ndimage.imread(fname, flatten=False))
    my_image = scipy.misc.imresize(image, size=(64, 64)).reshape((1, 64 * 64 * 3)).T
    my_image_prediction = predict(my_image, parameters)

    plt.imshow(image)
    print("Your algorithm predicts: y = " + str(np.squeeze(my_image_prediction)))
    plt.show()
コード例 #10
0
def GestureRecognition():
    learning_rate = 0.0001
    num_epochs = 1500
    minibatch_size = 32
    print_cost = True
    is_plot = True
    costs = []

    X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = tf_utils.load_dataset(
    )
    X_train_flatten = X_train_orig.reshape(X_train_orig.shape[0],
                                           -1).T  #每一列就是一个样本
    X_test_flatten = X_test_orig.reshape(X_test_orig.shape[0], -1).T

    m = X_train_flatten.shape[1]

    #归一化数据
    X_train = X_train_flatten / 255
    X_test = X_test_flatten / 255

    #转换为独热矩阵
    Y_train = tf_utils.convert_to_one_hot(Y_train_orig, 6)
    Y_test = tf_utils.convert_to_one_hot(Y_test_orig, 6)

    tf.set_random_seed(1)
    seed = 3
    tf.compat.v1.reset_default_graph()  # 用于清除默认图形堆栈并重置全局默认图形。

    X = tf.compat.v1.placeholder(tf.float32, [X_train.shape[0], None],
                                 name="X")
    Y = tf.compat.v1.placeholder(tf.float32, [Y_train.shape[0], None],
                                 name="Y")

    parameters = InitParameter([12288, 25, 12, 6])

    z = ForwardPropagation(X, parameters)
    cost = ComputeCost(z, Y)
    optimizer = tf.train.AdamOptimizer(
        learning_rate=learning_rate).minimize(cost)
    init = tf.global_variables_initializer()

    with tf.compat.v1.Session() as sess:
        sess.run(init)
        for epoch in range(num_epochs):
            epoch_cost = 0  # 每代的成本
            num_minibatches = int(m / minibatch_size)  # minibatch的总数量
            seed = seed + 1
            minibatches = tf_utils.random_mini_batches(X_train, Y_train,
                                                       minibatch_size, seed)

            for minibatch in minibatches:
                # 选择一个minibatch
                (minibatch_X, minibatch_Y) = minibatch

                # 数据已经准备好了,开始运行session
                _, minibatch_cost = sess.run([optimizer, cost],
                                             feed_dict={
                                                 X: minibatch_X,
                                                 Y: minibatch_Y
                                             })

                # 计算这个minibatch在这一代中所占的误差
                epoch_cost = epoch_cost + minibatch_cost / num_minibatches

            # 记录并打印成本
            ## 记录成本
            if epoch % 5 == 0:
                costs.append(epoch_cost)
                # 是否打印:
                if print_cost and epoch % 100 == 0:
                    print("epoch = " + str(epoch) + "    epoch_cost = " +
                          str(epoch_cost))

        # 是否绘制图谱
        if is_plot:
            plt.plot(np.squeeze(costs))
            plt.ylabel('cost')
            plt.xlabel('iterations (per tens)')
            plt.title("Learning rate =" + str(learning_rate))
            plt.show()

        # 保存学习后的参数
        parameters = sess.run(parameters)
        print("参数已经保存到session。")

        # 计算当前的预测结果
        correct_prediction = tf.equal(tf.argmax(z), tf.argmax(Y))

        # 计算准确率
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

        print("训练集的准确率:", accuracy.eval({X: X_train, Y: Y_train}))
        print("测试集的准确率:", accuracy.eval({X: X_test, Y: Y_test}))

        return parameters
コード例 #11
0
import math
import numpy as np
import h5py
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.python.framework import ops
from tf_utils import load_dataset, random_mini_batches, convert_to_one_hot, predict
import time

np.random.seed(1)

# Loading the dataset
X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, \
    classes = load_dataset()

# Example of a picture
index = 0
print("y = " + str(np.squeeze(Y_train_orig[:, index])))

# Flatten the training and test images
X_train_flatten = X_train_orig.reshape(X_train_orig.shape[0], -1).T
X_test_flatten = X_test_orig.reshape(X_test_orig.shape[0], -1).T
# Normalize image vectors
X_train = X_train_flatten / 255.
X_test = X_test_flatten / 255.
# Convert training and test labels to one hot matrices
Y_train = convert_to_one_hot(Y_train_orig, 6)
Y_test = convert_to_one_hot(Y_test_orig, 6)


def create_placeholders(n_x, n_y):
コード例 #12
0
# Author:yu
#使用tensorFlow构建神经网络
import numpy as np
import h5py
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.python.framework import ops
import tf_utils
import time
X_train_orig,Y_train_orig,X_test_orig,Y_test_orig,classes = tf_utils.load_dataset()
# index=11
# plt.imshow(X_train_orig[index])
# plt.show()
# print(np.squeeze(Y_train_orig))#从数组的形状中删除单维度条目,即把shape中为1的维度去掉

X_train_flatten = X_train_orig.reshape(X_train_orig.shape[0],-1).T#每一列就是一个样本
X_test_flatten = X_test_orig.reshape(X_test_orig.shape[0],-1).T

#归一化数据
X_train = X_train_flatten/255
X_test = X_test_flatten/255

#转换为独热矩阵
Y_train = tf_utils.convert_to_one_hot(Y_train_orig,6)
Y_test = tf_utils.convert_to_one_hot(Y_test_orig,6)

# print('训练集样本数 = '+str(X_train.shape[1]))
# print('测试集样本数 = '+str(X_test.shape[1]))
# print('X_train.shape:'+str(X_train.shape))
# print('Y_train.shape:'+str(Y_train.shape))
# print('X_test.shape:'+str(X_test.shape))
コード例 #13
0
# %%
sess = tf.Session()
one = tf.ones((3, 3))
zero = tf.zeros((3, 3))
y = np.array([[1, 2, 3, 0, 3, 4, 2, 1, 1]])
c = 5
one_hot_matrix = tf.one_hot(indices=np.squeeze(y), depth=c, axis=0)
# 参数可以是tensor对象, 可以是np.ndarray/int...python对象
# 返回对象会增加一个维度, 所以或者压缩y的维度, 或者reshape
print(f'one\n{sess.run(one)}')
print(f'one\n{sess.run(zero)}')
print(f'one_hot_matrix\n{sess.run(one_hot_matrix)}')

# %%
X_train, Y_train, X_test, Y_test, classes = tf_utils.load_dataset()

# %%
print(f'X_train.shape = {X_train.shape}')
print(f'Y_train.shape = {Y_train.shape}')
print(f'X_test.shape = {X_test.shape}')
print(f'Y_test.shape = {Y_test.shape}')
print(f'classes.shape = {classes.shape}')
print(f'classes = {classes}')

# %%
print(f'Y = {Y_train[0,11]}')
plt.imshow(X_train[11])

# %%
C = 6
コード例 #14
0
ファイル: TensorflowDemo.py プロジェクト: hamaswen/strategy
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from tf_utils import load_dataset, random_mini_batches, convert_to_one_hot, predict
from tensorflow.python.framework import ops
from sys import exit
from sklearn import preprocessing
import mpl_finance as mplf

# Loading the dataset
X_train_orig, Y_train_orig, X_test_orig, Y_test_orig = load_dataset()
'''
# Example of a K chart
index = 0
print("y = " + str(np.squeeze(Y_train_orig[:, index])))
print(X_train_orig[index].shape)
print(X_train_orig[index][:, :4].shape)
quotes = np.column_stack((range(20), X_train_orig[index][:, :4]))
quotes[:, [2, 3, 4]] = quotes[:, [3, 4, 2]]
fig, ax = plt.subplots()
mplf.candlestick_ohlc(ax, quotes, width=0.4, colorup='r', colordown='g')
plt.grid(False)
ax.autoscale_view()
plt.setp(plt.gca().get_xticklabels(), rotation=30)
plt.show()
'''
# Flatten the training and test images
X_train_flatten = X_train_orig.reshape(X_train_orig.shape[0], -1)
X_test_flatten = X_test_orig.reshape(X_test_orig.shape[0], -1)
# Normalize image vectors
print(X_train_flatten.shape)
コード例 #15
0
import math
import numpy as np
import h5py
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.python.framework import ops
from sklearn import preprocessing
from sklearn.preprocessing import OneHotEncoder
from tf_utils import load_dataset, convert_to_one_hot
from backwardPropagation import model
from keras.utils import to_categorical

X_train, X_test, y_train, y_test = load_dataset()

# Take transpose of the input data and also normalize it

X_train = X_train.T

X_train = (X_train - X_train.mean()) / (X_train.max() - X_train.min())
X_train = X_train.fillna(0)
X_test = X_test.T

X_test = (X_test - X_test.mean()) / (X_test.max() - X_test.min())
X_test = X_test.fillna(0)

# Convert training and test labels to one hot matrices

y_train = to_categorical(y_train, 9)
y_train = y_train.T

#print(y_train)