tensorflow笔记01

  1. 1. Tensorflow安装
  2. 2. TensorFlow常量、变量和占位符
  3. 3. 一些补充

Tensorflow安装

安装python,安装CUDA

CUDA版本号查看,打开英伟达控制面板,左下角系统信息

对应的CUDA11,网上的pip安装TensorFlow如果失败,或者想要用GPU的,可以用https://hub.fastgit.org/fo40225/tensorflow-windows-wheel这里有大佬编译好的包即可。

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
57
58
59
60
61
62
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 15 22:31:23 2021

@author: fshao
"""

import tensorflow as tf


def printMatrix(matrix):
print('===========================')
#输出
with tf.Session() as sess:
print(sess.run(matrix))

#声明标量常量
t_1 = tf.constant(4)

#常量向量
t_2 = tf.constant([4,3,2])

#创建所有元素为零的张量
t_3 = tf.zeros([4,5],tf.int32)
t_4 = tf.ones([4,5],tf.int32)

#在一定范围内生成一个start到stop的等差排布的序列
t_5 = tf.linspace(0.0,10.0,11)

#从开始(默认值=0)生成一个数字序列,增量为delta(默认值=1)
t_6 = tf.range(10)

#生成均值(默认为0.0) 标准差1.0 形状为[M,N]
t_7 = tf.random_normal([2,3],mean=2.0,stddev=1.0,seed=12)

#均值(默认0.0) 标准差(1.0) 形状为([M,N])的截尾正态分布随机数组
t_8 = tf.truncated_normal([1,5],stddev=2,seed=12)
#伽马分布随机数组
t_9 = tf.random_uniform([5,5],maxval=4,seed=12)

#将给定的张量随机裁剪为指定大小
t_10 = tf.random_crop(t_9,[2,3],seed=12)

#重新排列样本
t_11 = tf.random_shuffle(t_10)

#统一修改所有的随机种子值
tf.set_random_seed(54)

###########################变量#############################
weight = tf.Variable(tf.random_normal([100,100],stddev=2))
bias = tf.Variable(tf.zeros([100]),name = 'biases')


#########################占位符##############################
x = tf.placeholder("float")
y = 2 * x
data = tf.random_uniform([4,5],10)
with tf.Session() as sess:
x_data = sess.run(data)
print(sess.run(y,feed_dict={x:x_data}))
#printMatrix(t_11)

一些补充

1
2
3
4
5
6
7
8
9
10
import tensorflow as tf
#当需要转置时
tf.transpose([[1,2],[3,4]])
'''
结果:
1,3
2,4
'''
#当需要做类似于 0 if True else 1 python中的操作(相当于?:三元组操作)
tf.cond(pred,true_fn,false_fn)