#### (1)第一个Tensorflow程序
import tensorflow as tf
print(tf.__version__)
a=tf.constant(2.0)
print(a)
#### (2)Tensorflow2.x切换为1.x版本运行模式
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
a=tf.constant(2.4)
with tf.Session() as sess:
print(sess.run(a))
print(a)
#### (3)TensorFlow常量
import tensorflow as tf
#声明一个标题常量
t_1=tf.constant(2)
t_2=tf.constant(2)
#常量相加
t_add=tf.add(t_1,t_2)
#一个形如一行三列的常量向量可以用如下代码声明
t_3=tf.constant([4,3,2])
#定义一个形状为[M,N]的全0张量和全1张量
zero=tf.zeros(shape=[3,3])
ones=tf.ones(shape=[3,3])
print(t_add)
#### (4)TensorFlow变量
#直接赋值初始化
import tensorflow as tf
#直接给变量赋值初始化
bias1=tf.Variable(2)
#通过initial_value显示赋值初始化
bias2=tf.Variable(initial_value=3.)
bias1,bias2
#使用初始化函数初始化
import tensorflow as tf
a=tf.Variable(tf.zeros([2,1])) #将形状为[2,1]张量初始化为0
b=tf.Variable(tf.zeros_like(a)) #返回一个和给定tensor同样shape的tensor,其中的元素全部置0
c=tf.Variable(tf.ones([2,1])) #初始化为1
d=tf.Variable(tf.ones_like(a)) #将与a一个形状的张量初始化为1
e=tf.fill([2,3],4) #将指定形状的张量初始化为指定数值
print(a,b,c,d,e)
#### (5)张量(tensor)的属性
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
tensor = tf.constant([[1.0,2.0],[3.0,4.0]])
print("Tensor = ",tensor)
array = tensor.eval(session=tf.Session())
print(array)
#### (6)Tensor的基础运算操作
import tensorflow as tf
print(tf.add(1,2)) #0维向量相加
print(tf.add([1,2],[3,4])) #一维向量相加
print(tf.matmul([[1,2,3]],[[4],[5],[6]])) #矩阵相乘
print(tf.square(5)) #计算5的平方
print(tf.pow(2,3)) #计算2的3次方
print(tf.square(2)+tf.square(3)) #也支持操作符重载
print(tf.reduce_sum([1,2,3])) #计算数值的和
print(tf.reduce_mean([1,2,3])) #计算均值
#### (7)Tensor的其他操作
import tensorflow as tf
print(tf.argmax([1,0,0,8,6])) #返回数组内最大的索引,常用于处理one_hot向量
a=tf.constant([[1,2],[3,4],[5,6]])
b=tf.expand_dims(a,0) #在tensor中增加一个维度,0表示需要添加维度的下标为0
c=tf.expand_dims(a,1) #在tensor中增加一个维度,1表示需要添加的维度下标为1
print(a.shape,b.shape,c.shape)
x=[[1,2,3],[4,5,6],[7,8,9]]
y=[[2,3,4],[5,6,7],[8,9,10]]
z1=tf.concat([x,y],axis=0) #按照维度0进行拼接
z2=tf.concat([x,y],axis=1)
print(z1)
print(z2)
a=tf.Variable([[[1,2,3],[1,2,3]],[[7,8,9],[10,11,12]]])
b=tf.reshape(a,[6,2])
print(a.numpy())
print(b.numpy())
![复制代码]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
#序贯式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)
import torch
data=torch.rand(5,3)
print(data)