tensorflow学习笔记三----------基本操作
2024-09-05 19:33:33
tensorflow中的一些操作和numpy中的很像,下面列出几个比较常见的操作
import tensorflow as tf #定义三行四列的零矩阵
tf.zeros([3,4])
#定义两行三列的全1矩阵
tf.ones([2,3])
#定义常量
tensor = tf.constant([1,2,3,4,5,6,7])
#定义两行三列全为-1的矩阵
tensor = tf.constant(-1.0.shape=[2,3])
#[10 11 12]
tf.linspace(10.0,12.0,3,name="linespace") tf.range(start,end,delta)
#构造两行三列的均值为mean,方差为stddev的符合正态分布的矩阵
norm = tf.random_normal([2,3],mean=-1,stddev=4)
#洗牌操作
c = tf.constant([[1,2],[3,4],[5,6]])
shuff = tf.random_shuffle(c)
sess = tf.Session()
print(sess.run(norm))
print(sess.run(shuff))
#赋个初始值
state = tf.Variable(0)
#初始值加1
new_value = tf.add(state, tf.constant(1))
#更新
update = tf.assign(state, new_value)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(state))
for _ in range(3):
sess.run(update)
print(sess.run(state))
#numpy向tensorflow转换
import numpy as np
a = np.zeros((3,3))
ta = tf.convert_to_tensor(a)
with tf.Session() as sess:
print(sess.run(ta))
#tensorflow中的placeholder
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.multiply(input1,input2)
with tf.Session() as sess:
print(sess.run([output],feed_dict={input1:[7.],input2:[2.]}))
最新文章
- Android raw to bmp
- AO安装需要Microsoft Visual Studio 2013?
- internet选项菜单灰色
- FineUI第十二天---锚点布局
- storyboard有多个Segue的传递
- hibernate中many-to-one关联时出现ObjectNotFoundException异常
- rabbitmq之消息转储vm_memory_high_watermark_paging
- 第九天 内容提供者 ContentResolver
- Lazarus IDE的几个小技术
- [网络]远程访问局域网svn服务器[转]
- 《web全栈工程师的自我修养》阅读笔记
- 2 MySQL安装
- 关于iOS常用的26中公共方法,可copy的代码
- LindDotNetCore~添加路由前缀
- Python和C++的混合编程(使用Boost编写Python的扩展包)
- springboot项目简单启动脚本
- MySQL Network--Localhost与127.0.0.1的差异
- Docker的概述
- 自然语言处理--LDA主题聚类模型
- Qt5中运行后台网络读取线程与主UI线程互交