TensorFlow学习报告

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 11 19:10:39 2022
 
@author: 10320
"""
 
import tensorflow as tf
from tensorflow import keras
 
import numpy as np
import matplotlib.pyplot as plt
 
fashion_mnist = keras.datasets.fashion_mnist
 
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
 
 
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
train_images.shape
len(train_labels)
train_labels
test_images.shape
len(test_labels)
 
plt.figure()
plt.imshow(train_images[0])
plt.colorbar()
plt.grid(False)
plt.show()
train_images = train_images / 255.0
 
test_images = test_images / 255.0
 
plt.figure(figsize=(10,10))
for i in range(25):
    plt.subplot(5,5,i+1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(train_images[i], cmap=plt.cm.binary)
    plt.xlabel(class_names[train_labels[i]])
plt.show()
 
model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation='relu'),
    keras.layers.Dense(10)
])
 
model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])
 
model.fit(train_images, train_labels, epochs=10)

  

 

 

(1)TensorFlow和PyTorch

(2)可以直接赋值,也可以使用初始化函数

import tensorflow as tf

bias1=tf.Variable(2)

bias2=tf.Variable(initial_value=3.)

 

还有其他更加复杂的初始化方法 如:tf.zeros\tf.zeros_like\tf.ones_like\tf.random.truncated_normal等等

tf.random.truncated_normal和tf.zeros是常常用来进行权值和偏置的初始化方法

(3)序贯式、函数式

 

#序贯式1
import tensorflow as tf

model = tf.keras.Sequential()
#创建一个全连接层,神经元个数为256,输入为784,激活函数为relu
model.add(tf.keras.layers.Dense(256, activation='relu', input_dim=784))
model.add(tf.keras.layers.Dense(128, activation='relu'))
model.add(tf.keras.layers.Dense(10, activation='softmax'))

#序贯式2
import tensorflow as tf

imput_layer = tf.keras.layers.Input(shape=(784,))
hid1_layer = tf.keras.layers.Dense(256, activation='relu')
hid2_layer = tf.keras.layers.Dense(128, activation='relu')
output_layers = tf.keras.layers.Dense(10, activation='softmax') #将层的列表传给Sequential的构造函数
model = tf.keras.Sequential(layers=[imput_layer, hid1_layer, hid2_layer, output_layers])

 

#函数式
import tensorflow as tf
#创建一个模型,包含一个输入层和三个全连接层
inputs = tf.keras.layers.Input(shape=(4))
x=tf.keras.layers.Dense(32,activation='relu')(inputs)
x=tf.keras.layers.Dense(64,activation='relu')(x)
outputs=tf.keras.layers.Dense(3,activation='softmax')(x)
model=tf.keras.Model(inputs=inputs,outputs =outputs)

 

(4)

import torch
data=torch.rand(5,3)
print(data)

 

 

(5)Keras、Caffe、MXNet、Sonnet、Deeplearning4j

posted @   LianGiQ  阅读(36)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示