tensorflow基本语法

本文通过几个示例介绍了TensorFlow的基本用法,包括常量和变量的定义、矩阵乘法、算术运算及使用占位符进行计算。通过这些示例可以帮助读者快速上手TensorFlow。

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

#coding:utf-8
"""
学习tensorflow基本语法
"""
import tensorflow as tf
"""
a = tf.constant([[3,3]]) #tf.constant定义常量
b = tf.constant([[2],[2]])
c = tf.matmul(a,b) #matmul函数
with tf.Session() as sess:
    result = sess.run([c]) #需要进行run
    print result
"""
"""
state = tf.Variable(0,name='counter')#定义变量
one = tf.constant(1)
new_value = tf.add(state,one)
update = tf.assign(state,new_value) #tf.assign将new_value的值赋值给state
init_op = tf.initialize_all_variables() #初始化所有变量
with tf.Session() as sess:
    sess.run(init_op) #run变量
    for i in range(20):
        sess.run(update) #run
        print sess.run(state) #run
    """
"""
a = tf.constant(3)
b = tf.constant(2)
c = tf.constant(5)
num1 = tf.add(a,b)
num2 = tf.mul(c,num1)
with tf.Session() as sess:
    result = sess.run([num1,num2]) #run
    print result
"""
"""
input1 = tf.placeholder(tf.float32) #placeholder占位
input2 = tf.placeholder(tf.float32)
output = tf.mul(input1,input2)
with tf.Session() as sess:
    print sess.run([output],feed_dict={input1:[7],input2:[2]}) #feed_dict以字典的方式赋值
"""

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值