深度学习-卷积神经网络-tensorflow的用法-49

1. 01_first_graph

import tensorflow as tf

x = tf.Variable(3, name='x')
y = tf.Variable(4, name='y')

f = x * x * y + y + 2

sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))

sess.run(x.initializer)
sess.run(y.initializer)

result = sess.run(f)
print(result)
sess.close()

2. session run

import tensorflow as tf

x = tf.Variable(3, name='x')
y = tf.Variable(4, name='y')
f = x*x*y + y + 2

print(f)  # Tensor("add_1:0", shape=(), dtype=int32)  add_1:0代表第二个add操作   shape=() 没有维度 代表输出的是一个数


with tf.Session() as sess:
    x.initializer.run() # 等价 session.run(x.initializer)
    y.initializer.run()
    result = f.eval()  # 等价 session.run(f)

print(result)


3. global_variables_initializer

import tensorflow as tf

x = tf.Variable(3, name='x')
y = tf.Variable(4, name='y')
f = x*x*y + y + 2


init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)

    result = f.eval()

print(result)

4. InteractiveSession

import tensorflow as tf

x = tf.Variable(3, name='x')
y = tf.Variable(4, name='y')

f = x*x*y + y + 2

init = tf.global_variables_initializer()


sess = tf.InteractiveSession()

init.run()

result = f.eval()

print(result)

sess.close()

5. get_default_graph

import tensorflow as tf

x1 = tf.Variable(1)
print(x1.graph is tf.get_default_graph())

graph = tf.Graph()
x3 = tf.Variable(3)
with graph.as_default():
    x2 = tf.Variable(2)


x4 = tf.Variable(3)

print(x2.graph is graph)
print(x2.graph is tf.get_default_graph())

print(x3.graph is tf.get_default_graph())
print(x4.graph is tf.get_default_graph())

6. life_cicycle

import tensorflow as tf

w = tf.Variable(3)

x = w + 2
y = x + 5
z = y + 3

with tf.Session() as sess:
    sess.run(w.initializer)
    sess.run(y)

    sess.run(z)


with tf.Session() as sess:
    sess.run(w.initializer)
    y_val, z_val = sess.run([y, z])
    print(y_val, z_val)

07 linear_regression

import tensorflow as tf
import numpy as np
from sklearn.datasets import fetch_california_housing

housing = fetch_california_housing(data_home="./scikit_learn_data", download_if_missing=True)

m, n = housing.data.shape
print(m, n)
print(housing.data, housing.target)
print(housing.feature_names)

housing_data_plus_bias = np.c_[np.ones((m, 1)), housing.data]

X = tf.constant(housing_data_plus_bias, dtype=tf.float32, name='X')
y = tf.constant(housing.target.reshape(-1, 1), dtype=tf.float32, name='y')

XT = tf.transpose(X)

# (XT*X)**-1*XT*Y
theta = tf.matmul(tf.matmul(tf.matrix_inverse(tf.matmul(XT, X)), XT), y)

with tf.Session() as sess:
    theta_value = sess.run(theta)
print(theta_value)

8. manual_gradient

import tensorflow as tf
import numpy as np
from sklearn.datasets import fetch_california_housing
from sklearn.preprocessing import StandardScaler


housing = fetch_california_housing(data_home="./scikit_learn_data", download_if_missing=True)

n_epochs = 36500
learning_rate = 0.001

m, n = housing.data.shape
housing_data_plus_bias = np.c_[np.ones((m, 1)), housing.data]

# 归一化
scaler = StandardScaler(with_mean=True, with_std=True).fit(housing_data_plus_bias)
scaled_housing_data_plus_bias = scaler.transform(housing_data_plus_bias)
X = tf.constant(scaled_housing_data_plus_bias, dtype=tf.float32, name='X')
y = tf.constant(housing.target.reshape(-1,1), dtype=tf.float32, name='y')

# 初始化
theta = tf.Variable(tf.random_uniform([n+1, 1], -1, 1), name='theta')
y_pred = tf.matmul(X, theta, name='predictions')

error = y_pred - y

rmse = tf.sqrt(tf.reduce_mean(tf.square(error)), name='rmse')

# 梯度公式 (y_predict - y) * xj
gradients = 2/m*tf.matmul(tf.transpose(X), error)

# 训练的过程 就是更新theta
training_op = tf.assign(theta, theta - learning_rate * gradients)

init = tf.global_variables_initializer()

with tf.Session() as sess:

    sess.run(init)

    for epoch in range(n_epochs):

        if epoch % 100 == 0:
            print("Epoch", epoch, "RMSE=", rmse.eval())
        sess.run(training_op)

    best_theta = theta.eval()
    print(best_theta)




9. auto_diff

import tensorflow as tf
import numpy as np
from sklearn.datasets import fetch_california_housing
from sklearn.preprocessing import StandardScaler


housing = fetch_california_housing(data_home="./scikit_learn_data", download_if_missing=True)

n_epochs = 36500
learning_rate = 0.001

m, n = housing.data.shape
housing_data_plus_bias = np.c_[np.ones((m, 1)), housing.data]

# 归一化
scaler = StandardScaler(with_mean=True, with_std=True).fit(housing_data_plus_bias)
scaled_housing_data_plus_bias = scaler.transform(housing_data_plus_bias)
X = tf.constant(scaled_housing_data_plus_bias, dtype=tf.float32, name='X')
y = tf.constant(housing.target.reshape(-1,1), dtype=tf.float32, name='y')

# 初始化
theta = tf.Variable(tf.random_uniform([n+1, 1], -1, 1), name='theta')
y_pred = tf.matmul(X, theta, name='predictions')

error = y_pred - y

mse = tf.sqrt(tf.reduce_mean(tf.square(error)), name='mse')

# 梯度公式 (y_predict - y) * xj
# gradients = 2/m*tf.matmul(tf.transpose(X), error)
gradients = tf.gradients(mse, [theta])[0]
# 训练的过程 就是更新theta
training_op = tf.assign(theta, theta - learning_rate * gradients)

init = tf.global_variables_initializer()

with tf.Session() as sess:

    sess.run(init)

    for epoch in range(n_epochs):

        if epoch % 100 == 0:
            print("Epoch", epoch, "RMSE=", mse.eval())
        sess.run(training_op)

    best_theta = theta.eval()
    print(best_theta)




12. softmax_regression

from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf

my_mnist = input_data.read_data_sets("MNIST_data_bak/", one_hot=True)

# 55, 000 training data
# 10, 000 test data
# 5, 000 validate data

# 输入的是一堆图片,None表示不限输入条数,784表示每张图片都是一个784个像素值的一维向量 28*28
x = tf.placeholder(dtype=tf.float32, shape=(None, 784))
y = tf.placeholder(dtype=tf.float32, shape=(None, 10))
W = tf.Variable(tf.random_uniform([784, 10]))
b = tf.Variable(tf.zeros([10]))
y_predict = tf.nn.softmax(tf.matmul(x, W) + b)

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y * tf.log(y_predict), reduction_indices=[1]))

train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

# # tf.argmax()是一个从tensor中寻找最大值的序号,tf.argmax就是求各个预测的数字中概率最大的那一个 y 有归一化
correct_prediction = tf.equal(tf.argmax(y_predict, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

with tf.Session() as sess:
    tf.global_variables_initializer().run()
    for _ in range(10000):
        batch_xs, batch_ys = my_mnist.train.next_batch(100)
        sess.run(train_step, feed_dict={x: batch_xs, y: batch_ys})
        print("TrainSet batch acc : %s  " % accuracy.eval({x: batch_xs, y: batch_ys}))
        print("ValidSet acc : %s" % accuracy.eval({x: my_mnist.validation.images, y: my_mnist.validation.labels}))

    print("Test acc", accuracy.eval({x:my_mnist.test.images, y:my_mnist.test.labels}))

13. convolution

#!/usr/bin/python3
# -*- coding:utf-8 -*-
"""
Copyright (c) Huawei Technologies Co., Ltd. 2021-2022. All rights reserved.
"""

import numpy as np

from sklearn.datasets import load_sample_images
import tensorflow as tf
import matplotlib.pylab as plt

# 两张图片 每张图片 (height, width, channels)
# mini_batch dataset (batch_size, height, width, channels)
dataset = np.array(load_sample_images().images, dtype=np.float32)

batch_size, height, width, channels = dataset.shape
print("m: h: w: c====>", batch_size, height, width, channels)

# plt.imshow(load_sample_images().images[0])
# plt.show()
# plt.imshow(load_sample_images().images[1])
# plt.show()

# 初始化 2个 filters
filters_test = np.zeros(shape=(7, 7, channels, 2), dtype=np.float32)
filters_test[:, 3, :, 0] = 1  # 第一个核 中间列 第4列 设置为1
filters_test[3, :, :, 1] = 1  # 第二个核 中间行 第4行 设置为1

X = tf.placeholder(tf.float32, shape=(None, height, width, channels))
convolution = tf.nn.conv2d(X, filter=filters_test, strides=[1, 1, 1, 1],
                           padding="SAME")

with tf.Session() as sess:
    output = sess.run(convolution, feed_dict={X: dataset})
    print(output.shape)

plt.imshow(load_sample_images().images[0])
plt.show()

plt.imshow(output[0, :, :, 0])  # 第一张图的 第一个 feature_map
plt.show()

plt.imshow(output[0, :, :, 1])  # 第一张图的 第二个 feature_map
plt.show()

###########################

plt.imshow(load_sample_images().images[1])
plt.show()

plt.imshow(output[1, :, :, 0])  # 第二张图的 第一个 feature_map
plt.show()

plt.imshow(output[1, :, :, 1])  # 第二张图的 第二个 feature_map
plt.show()

14.pooling

import numpy as np
from sklearn.datasets import load_sample_images
import tensorflow as tf
import matplotlib.pylab as plt

dataset = np.array(load_sample_images().images, dtype=np.float32)

batch_size, height, width, channels = dataset.shape
print(batch_size, height, width, channels)


X = tf.placeholder(tf.float32, shape=(None, height, width, channels))


max_pool = tf.nn.max_pool(X, ksize=[1, 4, 4, 1], strides=[1, 4, 4, 1], padding="VALID")


with tf.Session() as sess:
    output = sess.run(max_pool, feed_dict={X: dataset})
    print(output.shape)

# 第一张图片 以及 池化后
plt.imshow(dataset[0].astype(np.uint8))
plt.show()
plt.imshow(output[0].astype(np.uint8))
plt.show()

# 第而张图片 以及 池化后
plt.imshow(dataset[1].astype(np.uint8))
plt.show()
plt.imshow(output[1].astype(np.uint8))
plt.show()





posted @ 2024-02-29 11:05  jack-chen666  阅读(3)  评论(0编辑  收藏  举报