示例#1
0
def declare_similar_tensor():
    show_title("TensorFlow 声明相似形状的张量")

    int_const_tsr = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
    show_values(int_const_tsr, "整数常数张量")

    zeros_similar_tsr = tf.zeros_like(int_const_tsr)
    show_values(zeros_similar_tsr, "相似形状的零张量")

    ones_similar_tsr = tf.ones_like(int_const_tsr)
    show_values(ones_similar_tsr, "相似形状的单位张量", session=sess)

    print('=' * 50)
    print("运算符重载")
    add_tsr = int_const_tsr + int_const_tsr
    show_values(add_tsr, "两个张量相加(int_const_tsr + int_const_tsr)", session=sess)

    multiply_tsr = int_const_tsr * int_const_tsr
    show_values(multiply_tsr,
                "两个张量相乘(int_const_tsr * int_const_tsr)",
                session=sess)

    neg_constant_tsr = -int_const_tsr
    show_values(neg_constant_tsr, "负张量(-int_const_tsr)", session=sess)

    number_multiply_tsr = 2 * int_const_tsr
    show_values(number_multiply_tsr, "数乘以张量(2 * int_const_tsr)", session=sess)

    abs_tsr = abs(neg_constant_tsr)
    show_values(abs_tsr, "张量取整(abs(neg_constant_tsr))", session=sess)

    minus_tsr = abs_tsr - neg_constant_tsr
    show_values(minus_tsr, "两个张量相减(abs_tsr - neg_constant_tsr)", session=sess)

    divide_tsr = multiply_tsr / neg_constant_tsr
    show_values(divide_tsr,
                "两个张量相除divide_tsr =(multiply_tsr / neg_constant_tsr)",
                session=sess)

    print('=' * 50)
    real_const_tsr = tf.constant([[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]],
                                 dtype=tf.float64)
    show_values(real_const_tsr, "浮点常数张量(real_const_tsr)", session=sess)

    a_one = real_const_tsr * real_const_tsr  # 这个是矩阵数乘,不是矩阵乘法
    show_values(a_one, "两个张量矩阵数乘(real_const_tsr*real_const_tsr)", session=sess)

    a_floor_div = real_const_tsr // divide_tsr
    show_values(a_floor_div,
                "两个张量整除(real_const_tsr // divide_tsr)",
                session=sess)

    a_mod = real_const_tsr % divide_tsr
    show_values(a_mod, "两个张量取余(real_const_tsr % divide_tsr)", session=sess)

    a_power = real_const_tsr**real_const_tsr
    show_values(a_power, "两个张量取幂(real_const_tsr ** divide_tsr)", session=sess)

    a_matrix_multipy = real_const_tsr @ real_const_tsr
    show_values(a_matrix_multipy, "两个张量矩阵乘(real_const_tsr @ real_const_tsr)")
示例#2
0
def declare_seq_tensor():
    show_title("TensorFlow 声明序列张量")

    linear_seq_tsr = tf.linspace(start=0.0, stop=1.0, num=3)
    show_values(linear_seq_tsr, "浮点序列张量", session=sess)

    integer_seq_tsr = tf.range(start=6, limit=15, delta=3)
    show_values(integer_seq_tsr, "整数序列张量", session=sess)
def declare_placeholder():
    show_title("TensorFlow 使用占位符")

    x = tf.placeholder(tf.float32, shape = (4, 4))
    y = tf.identity(x)  # 返回占位符传入的数据本身
    z = tf.matmul(y, x)

    x_vals = np.random.rand(4, 4)
    print("随机生成的原始张量(x_vals) = ")
    print(x_vals)
    show_values(y, "tf.identity(tf.placeholder(tf.float32, shape = (4,4)))", session = sess,
                feed_dict = {x: x_vals})
    show_values(tf.matmul(x_vals, x_vals), "tf.matmul(x_vals,x_vals)", session = sess)
    show_values(z, "tf.matmul(y,tf.placeholder(tf.float32, shape = (4,4)))", session = sess,
                feed_dict = {x: x_vals})
示例#4
0
def regression_example():
    show_title("反向传播算法的回归应用")
    # 创建数据集
    # x-data: 从正态分布 N(1, 0.1) 中抽取100个数据
    # target: 100个数据的对应目标值
    # x - data * A = target,理论上 A = 10.
    # 输入数据,因为是依据正态分布随机生成的数据,所以结果不可能得到完全一样的斜率
    x_vals = np.random.normal(1, 0.1, 100)
    # 预测数据,不同的预测数据,参数 A 会得到不同的结果
    # ToSee:注意下面三种目标设置导致的不同
    y_vals = np.repeat(10., 100)
    # y_vals = 3 + x_vals
    # y_vals = 3 * x_vals + 0.5
    plt.scatter(x_vals, y_vals)
    plt.title("原始数据的散点图")

    x_data = tf.placeholder(shape = [1], dtype = tf.float32)
    y_target = tf.placeholder(shape = [1], dtype = tf.float32)

    # A 是模型的参数(定义为变量)
    A = tf.Variable(tf.random_normal(shape = [1]))

    # 定义操作到计算图中,以下两种方式是一样的
    my_output = tf.multiply(x_data, A)
    my_output = x_data * A

    # 使用 L2 损失函数
    loss = tf.square(my_output - y_target)

    # Initialize variables
    init = tf.global_variables_initializer()
    sess.run(init)

    # 使用 梯度下降 优化器,目标是最小化损失
    learning_rate = 0.02
    my_opt = tf.train.GradientDescentOptimizer(learning_rate)
    train_step = my_opt.minimize(loss)

    # Run Loop
    for i in range(1000):
        rand_index = np.random.choice(100)
        rand_x = [x_vals[rand_index]]
        rand_y = [y_vals[rand_index]]
        feed_dict = {x_data: rand_x, y_target: rand_y}
        sess.run(train_step, feed_dict = feed_dict)
        if (i + 1) % 25 == 0:
            print('Step #' + str(i + 1) + ' A = ' + str(sess.run(A)))
            print('Loss = ' + str(sess.run(loss, feed_dict = feed_dict)))
示例#5
0
def declare_random_tensor():
    show_title("TensorFlow 声明随机张量")

    row_dim, col_dim = (6, 5)

    randunif_tsr = tf.random_uniform([row_dim, col_dim], minval=0, maxval=1)
    show_values(randunif_tsr, "均匀分布的随机数", session=sess)

    randnorm_tsr = tf.random_normal([row_dim, col_dim], mean=0.0, stddev=1.0)
    show_values(randnorm_tsr, "正态分布的随机数", session=sess)

    runcnorm_tsr = tf.truncated_normal([row_dim, col_dim],
                                       mean=0.0,
                                       stddev=1.0)
    show_values(runcnorm_tsr, "带有指定边界的正态分布的随机数", session=sess)

    shuffled_output = tf.random_shuffle(randunif_tsr)
    show_values(shuffled_output, "张量随机排序", session=sess)

    cropped_output = tf.random_crop(randunif_tsr, [3, 4])
    show_values(cropped_output, "张量的随机剪裁", session=sess)
def declare_variable():
    show_title("TensorFlow 使用变量")

    # Declare a variable
    my_var = tf.Variable(tf.zeros([1, 20]))

    print("全局初始化变量")
    # init = tf.initialize_all_variables()  # 这个已经被废弃了
    initialize_op = tf.global_variables_initializer()  # Initialize operation
    sess.run(initialize_op)  # Run initialization of variable

    print("my_var = ", my_var)
    print("sess.run(my_var)", sess.run(my_var))

    show_values(initialize_op, "initialize_op", session = sess)
    # 不同的 session ,不同的环境初始化。
    show_values(my_var, "my_var", session = sess)

    print('-' * 50)
    print("每个变量独自初始化。。。")
    first_var = tf.Variable(tf.zeros([2, 3]))
    print("first_var", first_var)
    print("first_var.initializer = ", first_var.initializer)
    print("sess.run(first_var.initializer) = ", sess.run(first_var.initializer))
    print("sess.run(first_var) = \n", sess.run(first_var))
    show_values(first_var, "first_var", session = sess)
    show_values(first_var.initializer, "first_var.initializer", session = sess)

    print('-' * 50)
    second_var = tf.Variable(tf.ones_like(first_var))
    print("second_var", second_var)
    print("second_var.initializer", second_var.initializer)
    print("sess.run(second_var.initializer) = ", sess.run(second_var.initializer))
    print("sess.run(second_var) = \n", sess.run(second_var))
    show_values(second_var.initializer, "second_var.initializer", session = sess)
    show_values(second_var, "second_var", session = sess)
# 利用随机种子,保证随机数据的稳定性,使得每次随机测试的结果一样
np.random.seed(42)

# 初始化默认的计算图
ops.reset_default_graph()
# Python ≥3.5 is required
assert sys.version_info >= (3, 5)
# Scikit-Learn ≥0.20 is required
assert sklearn.__version__ >= "0.20"
# 屏蔽警告:Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
# Open graph session
sess = tf.Session()

show_title("TensorFlow 创建分类器")

# Load the iris data(150条数据)
# iris.target = {0, 1, 2}, where '0' is setosa
# iris.data ~ ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']
# 实现一个二值分类器,预测一朵花是否为山鸢尾(setosa, iris.target==0, binary_target==1.0)
# 只使用两个特征:花瓣长度(pedal.length,x[2])和花瓣宽度(pedal.width,x[3])
iris = load_iris()
binary_target = np.array([1. if x == 0 else 0. for x in iris.target])
iris_2d = np.array([[x[2], x[3]] for x in iris.data])

# Declare batch size
batch_size = 20

# Declare placeholders
x1_data = tf.placeholder(shape=[None, 1], dtype=tf.float32)
# 初始化默认的计算图
ops.reset_default_graph()
# Python ≥3.5 is required
assert sys.version_info >= (3, 5)
# Scikit-Learn ≥0.20 is required
assert sklearn.__version__ >= "0.20"
# 屏蔽警告:Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
# Open graph session
sess = tf.Session()

# -----------------------------------------------------------------
# Stochastic Training:一次训练所有的数据,但是数据随机打乱顺序,
# 训练结果不平稳,训练数据量大,计算速度相对较快
show_title("TensorFlow 随机训练")

# Create data
x_vals = np.random.normal(1, 0.1, 100)
y_vals = np.repeat(10., 100)
x_data = tf.placeholder(shape=[1], dtype=tf.float32)
y_target = tf.placeholder(shape=[1], dtype=tf.float32)

# Create variable (one model parameter = A)
A = tf.Variable(tf.random_normal(shape=[1]))

# Add operation to graph
my_output = tf.multiply(x_data, A)

# Add L2 loss operation to graph
loss = tf.square(my_output - y_target)
def regression_model():
    show_title("回归模型")
    # Declare batch size
    batch_size = 25

    # 创建数据集
    # x-data: 从正态分布 N(1, 0.1) 中抽取100个数据
    # target: 100个数据的对应目标值
    # x - data * A = target,理论上 A = 10.
    x_vals = np.random.normal(1, 0.1, 100)
    y_vals = np.repeat(10., 100)
    x_data = tf.placeholder(shape=[None, 1], dtype=tf.float32)
    y_target = tf.placeholder(shape=[None, 1], dtype=tf.float32)

    # 将数据拆分:训练集占80%;测试集占20%
    train_indices = np.random.choice(len(x_vals),
                                     int(round(len(x_vals) * .8)),
                                     replace=False)
    test_indices = np.array(list(set(range(len(x_vals))) - set(train_indices)))
    x_vals_train = x_vals[train_indices]
    y_vals_train = y_vals[train_indices]
    x_vals_test = x_vals[test_indices]
    y_vals_test = y_vals[test_indices]

    # Create variable (one model parameter = A)
    A = tf.Variable(tf.random_normal(shape=[1, 1]))

    # Add operation to graph
    my_output = tf.matmul(x_data, A)

    # Add L2 loss operation to graph
    loss = tf.reduce_mean(tf.square(my_output - y_target))

    # Initialize variables
    init = tf.global_variables_initializer()
    sess.run(init)

    # Create Optimizer
    my_opt = tf.train.GradientDescentOptimizer(0.02)
    train_step = my_opt.minimize(loss)

    # Run Loop
    for i in range(1000):
        rand_index = np.random.choice(len(x_vals_train), size=batch_size)
        rand_x = np.transpose([x_vals_train[rand_index]])
        rand_y = np.transpose([y_vals_train[rand_index]])
        sess.run(train_step, feed_dict={x_data: rand_x, y_target: rand_y})
        if i % 111 == 0:
            print('Step #' + str(i + 1) + ' A = ' + str(sess.run(A)))
            print('Loss = ' + str(
                sess.run(loss, feed_dict={
                    x_data: rand_x,
                    y_target: rand_y
                })))

    # Evaluate accuracy (loss) on test set
    print('-' * 50)
    mse_train = sess.run(loss,
                         feed_dict={
                             x_data: np.transpose([x_vals_train]),
                             y_target: np.transpose([y_vals_train])
                         })
    mse_test = sess.run(loss,
                        feed_dict={
                            x_data: np.transpose([x_vals_test]),
                            y_target: np.transpose([y_vals_test])
                        })
    print("训练集的MSE =" + str(np.round(mse_train, 2)))
    print("测试集的MSE =" + str(np.round(mse_test, 2)))
    print("测试集的MSE 比训练集的 MSE 小,是因为测试集的数据集比训练集的数据集小")
    print("正确的评估模型精度的方法,是寻找一个基准模型,比基本模型的效果好就证明成功了")
def classification_model():
    show_title("分类模型")
    # Declare batch size
    batch_size = 25

    # 创建的数据集
    # x-data: 从 N(-1, 1) 中抽取50个随机样本 + 从 N(1, 1) 中抽取50个随机样本
    # target: 前面 50 个样本对应标签为0,后面50个样本对应标签为1
    # 训练二分类模型
    # If sigmoid(x+A) < 0.5 -> 0 else 1,理论上: A = -(mean1 + mean2)/2
    x_vals = np.concatenate((np.random.normal(-1, 1,
                                              50), np.random.normal(2, 1, 50)))
    y_vals = np.concatenate((np.repeat(0., 50), np.repeat(1., 50)))
    x_data = tf.placeholder(shape=[1, None], dtype=tf.float32)
    y_target = tf.placeholder(shape=[1, None], dtype=tf.float32)

    # 将数据拆分:训练集占80%;测试集占20%
    train_indices = np.random.choice(len(x_vals),
                                     int(round(len(x_vals) * 0.8)),
                                     replace=False)
    test_indices = np.array(list(set(range(len(x_vals))) - set(train_indices)))
    x_vals_train = x_vals[train_indices]
    x_vals_test = x_vals[test_indices]
    y_vals_train = y_vals[train_indices]
    y_vals_test = y_vals[test_indices]

    # Create variable (one model parameter = A)
    A = tf.Variable(tf.random_normal(mean=10, shape=[1]))

    # Add operation to graph
    # Want to create the operstion sigmoid(x + A)
    # Note, the sigmoid() part is in the loss function
    my_output = tf.add(x_data, A)

    # Initialize variables
    init = tf.global_variables_initializer()
    sess.run(init)

    # Add classification loss (cross entropy)
    xentropy = tf.reduce_mean(
        tf.nn.sigmoid_cross_entropy_with_logits(logits=my_output,
                                                labels=y_target))

    # Create Optimizer
    my_opt = tf.train.GradientDescentOptimizer(0.05)
    train_step = my_opt.minimize(xentropy)

    # Run loop
    for i in range(2800):
        rand_index = np.random.choice(len(x_vals_train), size=batch_size)
        rand_x = [x_vals_train[rand_index]]
        rand_y = [y_vals_train[rand_index]]
        sess.run(train_step, feed_dict={x_data: rand_x, y_target: rand_y})
        if (i + 1) % 200 == 0:
            print('Step #' + str(i + 1) + ' A = ' + str(sess.run(A)))
            print('Loss = ' + str(
                sess.run(xentropy,
                         feed_dict={
                             x_data: rand_x,
                             y_target: rand_y
                         })))

    # Evaluate Predictions on test set
    y_prediction = tf.squeeze(tf.round(tf.nn.sigmoid(tf.add(x_data, A))))
    correct_prediction = tf.equal(y_prediction, y_target)
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    acc_value_train = sess.run(accuracy,
                               feed_dict={
                                   x_data: [x_vals_train],
                                   y_target: [y_vals_train]
                               })
    acc_value_test = sess.run(accuracy,
                              feed_dict={
                                  x_data: [x_vals_test],
                                  y_target: [y_vals_test]
                              })
    print("Accuracy on train set: " + str(acc_value_train))
    print("Accuracy on test set: " + str(acc_value_test))

    # Plot classification result
    A_result = -sess.run(A)
    bins = np.linspace(-5, 5, 500)
    plt.hist(x_vals[0:500], bins, alpha=0.5, label='N(-1,1)', color='blue')
    plt.hist(x_vals[500:1000], bins, alpha=0.5, label='N(2,1)', color='red')
    plt.plot((A_result, A_result), (0, 8),
             'k--',
             linewidth=3,
             label='A = ' + str(np.round(A_result, 2)))
    plt.legend(loc='upper right')
    plt.suptitle("图2-8:模型A和数据点的可视化——两个正态分布(均值为-1和2)。\n"
                 "理论上的最佳分割点是(0.5),模型结果为({})。".format(A_result))
示例#11
0
def convolution_1d_data():
    show_title("1-D 序列数据")

    # Reset Graph
    ops.reset_default_graph()
    sess = tf.Session()

    # Generate 1D data
    data_size = 25
    data_1d = np.random.normal(size=data_size)

    # Placeholder
    x_input_1d = tf.placeholder(dtype=tf.float32, shape=[data_size])

    # --------Convolution--------
    def conv_layer_1d(input_1d, my_filter):
        # Tensorflow 的层函数是为四维数据设计的(batch size, width, height, channels)
        # expand_dims() 用于扩展维度;squeeze() 用于降低维度
        input_2d = tf.expand_dims(input_1d, 0)
        input_3d = tf.expand_dims(input_2d, 0)
        input_4d = tf.expand_dims(input_3d, 3)

        # tf.nn.conv2d (input, filter, strides, padding, use_cudnn_on_gpu=None, data_format=None, name=None)
        # input : 输入的要做卷积的图片,要求为一个张量,shape为 [ batch, in_height, in_weight, in_channel ],其中batch为图片的数量,in_height 为图片高度,in_weight 为图片宽度,in_channel 为图片的通道数,灰度图该值为1,彩色图为3。(也可以用其它值,但是具体含义不是很理解)
        # filter: 卷积核,要求也是一个张量,shape为 [ filter_height, filter_weight, in_channel, out_channels ],其中 filter_height 为卷积核高度,filter_weight 为卷积核宽度,in_channel 是图像通道数 ,和 input 的 in_channel 要保持一致,out_channel 是卷积核数量。
        # strides: 卷积时在图像每一维的步长,这是一个一维的向量,[ 1, strides, strides, 1],第一位和最后一位固定必须是1
        # padding: string类型,值为“SAME” 和 “VALID”,表示的是卷积的形式,是否考虑边界。"SAME"是考虑边界,不足的时候用0去填充周围,"VALID"则不考虑
        # use_cudnn_on_gpu: bool类型,是否使用cudnn加速,默认为true
        convolution_output = tf.nn.conv2d(input_4d,
                                          filter=my_filter,
                                          strides=[1, 1, 1, 1],
                                          padding='VALID')
        conv_output_1d = tf.squeeze(convolution_output)  # 去除增加的维度
        return conv_output_1d

    # 创建卷积使用的滤波器,自动生成的参数,训练中会改变
    my_filter = tf.Variable(tf.random_normal(shape=[1, 5, 1, 1]))
    my_convolution_output = conv_layer_1d(x_input_1d, my_filter)

    # --------Activation--------
    def activation(input_1d):
        return tf.nn.relu(input_1d)

    my_activation_output = activation(my_convolution_output)

    # --------Max Pool--------
    def max_pool(input_1d, width):
        # Tensorflow 的 max_pool() 函数使用 4D 数据:[batch_size, width, height, channels]
        # batch:为数据的批量个数
        # in_height:图片高度
        # in_weight 为图片宽度
        # in_channel 为图片的通道数,灰度图该值为1(0到255表示从黑到白),彩色图为3(红、绿、蓝)
        # 因此需要把数据从2D扩展到4D
        input_2d = tf.expand_dims(input_1d, 0)
        input_3d = tf.expand_dims(input_2d, 0)
        input_4d = tf.expand_dims(input_3d, 3)

        # tf.nn.max_pool(value, ksize, strides, padding, name=None)
        # value:需要池化的输入,一般池化层接在卷积层后面,所以输入通常是feature map,依然是[batch, height, width, channels]这样的shape
        # ksize:池化窗口的大小,取一个四维向量,一般是[1, height, width, 1],因为我们不想在batch和channels上做池化,所以这两个维度设为了1
        # strides:和卷积类似,窗口在每一个维度上滑动的步长,一般也是[1, stride,stride, 1]
        # padding:和卷积类似,可以取'VALID' 或者'SAME'
        # 返回:Tensor,类型不变,shape仍然是[batch, height, width, channels]这种形式
        pool_output = tf.nn.max_pool(input_4d,
                                     ksize=[1, 1, width, 1],
                                     strides=[1, 1, 1, 1],
                                     padding='VALID')
        pool_output_1d = tf.squeeze(pool_output)
        return pool_output_1d

    my_max_pool_output = max_pool(my_activation_output, width=5)

    # --------Fully Connected--------
    def fully_connected(input_layer, num_outputs):
        # 全连接层需要把2D数据展开成1D数据
        # TF1.2.1 tf.stack() 替代 tf.pack()
        weight_shape = tf.squeeze(
            tf.stack([tf.shape(input_layer), [num_outputs]]))  # 权重矩阵的形状
        weight = tf.random_normal(weight_shape, stddev=0.1)  # 初始化权重矩阵
        bias = tf.random_normal(shape=[num_outputs])  # 初始化偏移量
        input_layer_2d = tf.expand_dims(
            input_layer, 0)  # 把 flat_input 展开成2D,才能与 weight 矩阵相乘
        full_output = (input_layer_2d @ weight) + bias
        # full_output_2d = tf.squeeze(full_output)  # 输出是1D,不需要再压缩维度
        full_output_1d = full_output
        return full_output_1d

    my_full_output = fully_connected(my_max_pool_output, 5)

    # Run graph
    # Initialize Variables
    init = tf.global_variables_initializer()
    sess.run(init)

    feed_dict = {x_input_1d: data_1d}

    # Convolution Output
    print("Data = 序列长度 25")
    print(data_1d)
    print('-' * 50)
    print("\nFilter(滤波器) = ", my_filter.get_shape())
    print(sess.run(my_filter))

    print('-' * 50)
    print('\nInput = 序列长度 25')
    print("卷积输出(序列长度=21,滤波器长度=5,步长=1):")
    run_my_convolution_output = sess.run(my_convolution_output,
                                         feed_dict=feed_dict)
    print(run_my_convolution_output)

    print('-' * 50)
    print('\nInput = 序列长度 21')
    print('ReLU 激活函数输出(序列长度=21):')
    run_my_activation_output = sess.run(my_activation_output,
                                        feed_dict=feed_dict)
    print(run_my_activation_output)

    print('-' * 50)
    print('\nInput = 序列长度 21')
    print('卷积输出(序列长度=17,滤波器长度=5,步长=1):')
    run_my_max_pool_output = sess.run(my_max_pool_output, feed_dict=feed_dict)
    print(run_my_max_pool_output)

    print('-' * 50)
    print('\nInput = 序列长度 17')
    print('全连接层输出(序列长度=5):')
    run_my_full_output = sess.run(my_full_output, feed_dict=feed_dict)
    print(run_my_full_output)

    print('-' * 50)
    print('\n模型输出序列长度=5')
    pass
示例#12
0
def convolution_2d_data():
    show_title("2-D 图片数据")

    # Reset Graph
    ops.reset_default_graph()
    sess = tf.Session()

    # Generate 2D data
    data_size = [10, 10]
    data_2d = np.random.normal(size=data_size)

    # --------Placeholder--------
    x_input_2d = tf.placeholder(dtype=tf.float32, shape=data_size)

    # Convolution
    def conv_layer_2d(input_2d, my_filter):
        input_3d = tf.expand_dims(input_2d, 0)
        input_4d = tf.expand_dims(input_3d, 3)
        convolution_output = tf.nn.conv2d(input_4d,
                                          filter=my_filter,
                                          strides=[1, 2, 2, 1],
                                          padding='VALID')
        conv_output_2d = tf.squeeze(convolution_output)
        return conv_output_2d

    # Create Convolutional Filter
    my_filter = tf.Variable(tf.random_normal(shape=[
        2,
        2,
        1,
        1,
    ]))
    # Create Convolutional Layer
    my_convolution_output = conv_layer_2d(x_input_2d, my_filter)

    # --------Activation--------
    def activation(input_2d):
        return tf.nn.relu(input_2d)

    # Create Activation Layer
    my_activation_output = activation(my_convolution_output)

    # --------Max Pool--------
    def max_pool(input_2d, width, height):
        input_3d = tf.expand_dims(input_2d, 0)
        input_4d = tf.expand_dims(input_3d, 3)
        pool_output = tf.nn.max_pool(input_4d,
                                     ksize=[1, height, width, 1],
                                     strides=[1, 1, 1, 1],
                                     padding='VALID')
        pool_output_2d = tf.squeeze(pool_output)  # 去除增加的维度
        return pool_output_2d

    # Create Max-Pool Layer
    my_max_pool_output = max_pool(my_activation_output, width=2, height=2)

    # --------Fully Connected--------
    def fully_connected(input_layer, num_outputs):
        flat_input = tf.reshape(input_layer, [-1])
        # We then find out how long it is, and create an array for the shape of
        # the multiplication weight = (WxH) by (num_outputs)
        weight_shape = tf.squeeze(
            tf.stack([tf.shape(flat_input), [num_outputs]]))
        weight = tf.random_normal(weight_shape, stddev=0.1)
        bias = tf.random_normal(shape=[num_outputs])
        input_2d = tf.expand_dims(flat_input, 0)
        full_output = input_2d @ weight + bias
        full_output_2d = full_output
        return full_output_2d

    # Create Fully Connected Layer
    my_full_output = fully_connected(my_max_pool_output, 5)

    # Run graph
    # Initialize Variables
    init = tf.global_variables_initializer()
    sess.run(init)

    feed_dict = {x_input_2d: data_2d}

    print('-' * 50)
    print('Input = 输入 [10 X 10] array')
    print('2x2 卷积层, stride size = [2x2], 输出 [5x5] array:')
    print(sess.run(my_convolution_output, feed_dict=feed_dict))

    # Activation Output
    print('\nInput = my_convolution_output [5x5] array')
    print('ReLU 输出 [5x5] array:')
    print(sess.run(my_activation_output, feed_dict=feed_dict))

    # Max Pool Output
    print('\nInput = my_activation_output [5x5] array')
    print('MaxPool, stride size = [1x1], 输出 [4x4] array:')
    print(sess.run(my_max_pool_output, feed_dict=feed_dict))

    # Fully Connected Output
    print('\nInput = my_max_pool_output [4x4] array')
    print('全连接层将 [4x4] 展开成 1D 长度=16,输出长度=5:')
    print(sess.run(my_full_output, feed_dict=feed_dict))
    pass
# Open graph session
sess = tf.Session()

# Load the data
# iris.data = [(Sepal Length, Sepal Width, Petal Length, Petal Width)]
# 从这一节开始,都是使用花瓣宽度来拟合花萼长度
iris = load_iris()
x_vals = np.array([x[3] for x in iris.data])  # 花瓣宽度
y_vals = np.array([y[0] for y in iris.data])  # 花萼长度

# Declare batch size and number of iterations
batch_size = 25
learning_rate = 0.04  # Will not converge with learning rate at 0.4
iterations = 501

show_title("TensorFlow L1 Loss 代价损失函数的线性回归算法")

# Initialize placeholders
x_data = tf.placeholder(shape=[None, 1], dtype=tf.float32)
y_target = tf.placeholder(shape=[None, 1], dtype=tf.float32)

# Create variables for linear regression
A = tf.Variable(tf.random_normal(shape=[1, 1]))
b = tf.Variable(tf.random_normal(shape=[1, 1]))

# Initialize variables
init = tf.global_variables_initializer()
sess.run(init)

# Declare model operations
model_output = x_data @ A + b
# -----------------------------------------------------------------
# Load the data
# iris.data = [(Sepal Length, Sepal Width, Petal Length, Petal Width)]
# 鸢尾花(3种),特征4种(花萼长度、花萼宽度、花瓣长度、花瓣宽度),150条数据
iris = load_iris()
x_vals = np.array([x[3] for x in iris.data])
y_vals = np.array([y[0] for y in iris.data])

# Declare batch size
batch_size = 50
learning_rate = 0.001
iterations = 5001  # 多于2000次以后,好像才能收敛

# -----------------------------------------------------------------
show_title("TensorFlow Lasso Loss 代价损失函数的线性回归算法")

# Initialize placeholders
x_data = tf.placeholder(shape=[None, 1], dtype=tf.float32)
y_target = tf.placeholder(shape=[None, 1], dtype=tf.float32)

# Create variables for linear regression
A = tf.Variable(tf.random_normal(shape=[1, 1]))
b = tf.Variable(tf.random_normal(shape=[1, 1]))

# Declare model operations
model_output_lasso = x_data @ A + b
# model_output_lasso = tf.add(tf.matmul(x_data, A), b)

# Declare Lasso loss function(改良过的连续阶跃函数,Lasso回归的截止点设为0.9,即斜率系数不超过0.9)
# Lasso Loss = L2_Loss + heavy_side_step,
# 利用随机种子,保证随机数据的稳定性,使得每次随机测试的结果一样
np.random.seed(42)

# 初始化默认的计算图
ops.reset_default_graph()
# Python ≥3.5 is required
assert sys.version_info >= (3, 5)
# Scikit-Learn ≥0.20 is required
assert sklearn.__version__ >= "0.20"
# 屏蔽警告:Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
# Open graph session
sess = tf.Session()

# 1.5 矩阵
show_title("TensorFlow 声明矩阵")

identity_matrix = tf.diag([1.0, 1.0, 1.0])
show_values(identity_matrix, "单位矩阵")

A = tf.truncated_normal(shape=[12, 13])
show_values(A, "A = 12x13 truncated random normal distribution")

B = tf.fill([12, 13], 5.0)
show_values(B, "B = 12x13 constant matrix, fill matrix with 5.0")

C = tf.random_uniform(shape=[13, 12])
show_values(C, "C = 13x12 random uniform distribution")
show_values(C, "再运行一次C = 13x12 random uniform distribution")

D = tf.convert_to_tensor(
示例#16
0
def classification_example():
    show_title("反向传播算法的分类问题应用")
    # 创建的数据集
    # x-data: 从 N(-1, 1) 中抽取50个随机样本 + 从 N(1, 1) 中抽取50个随机样本
    # target: 前面 50 个样本对应标签为0,后面50个样本对应标签为1
    # 训练二分类模型
    # If sigmoid(x+A) < 0.5 -> 0 else 1,理论上: A = -(mean1 + mean2)/2
    x_vals = np.concatenate((np.random.normal(-1, 1, 50), np.random.normal(3, 1, 50)))
    y_vals = np.concatenate((np.repeat(0., 50), np.repeat(1., 50)))
    plt.scatter(x_vals, y_vals)
    plt.title("原始数据的散点图")

    x_data = tf.placeholder(shape = [1], dtype = tf.float32)
    y_target = tf.placeholder(shape = [1], dtype = tf.float32)

    # Create variable (one model parameter = A)
    # A 是模型参数
    A = tf.Variable(tf.random_normal(mean = 10, shape = [1]))

    # Add operation to graph
    # Want to create the operation sigmoid(x + A)
    # Note, the sigmoid() part is in the loss function
    # sigmoid()在代价函数中定义
    # 注:这个例子中定义 sigmoid() 的方法不好,代码不清晰,特别是对于相对复杂的代码来说,阅读起来有困难
    # 保留在这里不做修改,可以思考如何定义更好
    my_output = x_data + A
    # Now we have to add another dimension to each (batch size of 1)
    # 因为指定的损失函数期望批量数据,所以增加一个维度用于定义批量的大小(批量大小默认为1)
    my_output_expanded = tf.expand_dims(my_output, 0)
    y_target_expanded = tf.expand_dims(y_target, 0)

    # Initialize variables
    init = tf.global_variables_initializer()
    sess.run(init)

    # Add classification loss (cross entropy)
    # 原代码把输入数据和目标数据放反了。
    # xentropy = tf.nn.sigmoid_cross_entropy_with_logits(labels = my_output_expanded, logits = y_target_expanded)
    xentropy = tf.nn.sigmoid_cross_entropy_with_logits(logits = my_output_expanded, labels = y_target_expanded)
    # ToDo: 可以尝试更换其他代价函数
    # xentropy = - (y_target_expanded @ tf.log(my_output_expanded)) \
    #            - ((1. - y_target_expanded) @ tf.log(1. - my_output_expanded))
    # xentropy = -(y_target * tf.log(my_output)) - ((1. - y_target) * tf.log(1. - my_output))
    # xentropy = tf.maximum(0., 1. - tf.multiply(y_target_expanded, my_output_expanded))
    # xentropy= tf.maximum(0., 1. - tf.multiply(y_vals, x_vals))

    # Create Optimizer
    # 使用梯度下降 优化器
    learning_rate = 0.05
    my_opt = tf.train.GradientDescentOptimizer(learning_rate)
    train_step = my_opt.minimize(xentropy)

    # Run loop
    for i in range(1400):
        rand_index = np.random.choice(100)
        rand_x = [x_vals[rand_index]]
        rand_y = [y_vals[rand_index]]

        sess.run(train_step, feed_dict = {x_data: rand_x, y_target: rand_y})
        if (i + 1) % 200 == 0:
            print('Step #' + str(i + 1) + ' A = ' + str(sess.run(A)))
            print('Loss = ' + str(sess.run(xentropy, feed_dict = {
                    x_data: rand_x, y_target: rand_y
            })))

    # Evaluate Predictions
    predictions = []
    for i in range(len(x_vals)):
        x_val = [x_vals[i]]
        prediction = sess.run(tf.round(tf.sigmoid(my_output)), feed_dict = {x_data: x_val})
        predictions.append(prediction[0])

    accuracy = sum(x == y for x, y in zip(predictions, y_vals)) / 100.
    print('评估精度 = ' + str(np.round(accuracy, 2)))