深度学习笔记——tensorflow的使用

本文详细介绍了使用TensorFlow进行基本操作的方法,包括常数、占位符、会话和操作的使用,以及如何通过矩阵乘法进行数据处理。通过实例演示了如何创建和运行会话,执行加减乘除等数学运算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

tensorflow的安装就不用多说了
直接pip install tensorflow=版本号
或者直接在VSCode终端conda install tensorflow可以快速安装最新版tensorflow

接下来我们来学习一些tensorflow的基本操作

Constant (常数)

import tensorflow as tf
x = tf.constant(100)
x
type(x)

<tf.Tensor ‘Const_3:0’ shape=() dtype=int32>
tensorflow.python.framework.ops.Tensor

Session (会话)

sess = tf.Session()
sess.run(x)

100

type(sess.run(x))

numpy.int32

Operation (操作)

x = tf.constant(2)
y = tf.constant(3)
with tf.Session() as sess:
    print(sess.run(x+y))
    print(sess.run(x-y))
    print(sess.run(x*y))
    print(sess.run(x/y))

Placeholder (占位符)

x = tf.placeholder(tf.int32)
y = tf.placeholder(tf.int32)
x
type(x)

<tf.Tensor ‘Placeholder:0’ shape= dtype=int32>
tensorflow.python.framework.ops.Tensor

定义操作

add = tf.add(x,y)
sub = tf.subtract(x,y)
mul = tf.multiply(x,y)
d = {x:20,y:30}
with tf.Session() as sess:
    print(sess.run(add,feed_dict=d))
    print(sess.run(sub,feed_dict=d))
    print(sess.run(mul,feed_dict=d))

50
-10
600

import numpy as np
a = np.array([[5.0,5.0]])
b = np.array([[2.0],[2.0]])
a
a.shape
b
b.shape

array([[5., 5.]])
(1, 2)
array([[2.],
[2.]])
(2, 1)

矩阵乗积

mat1 = tf.constant(a)
mat2 = tf.constant(b)
matrix_multi = tf.matmul(mat1,mat2)
with tf.Session() as sess:
    result = sess.run(matrix_multi)
    print(result)

[[20.]]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值