Tensorflow中的基本加法?

2024-04-29 04:00:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一个程序,我输入一组x1 x2,输出一个y。我能找到的所有张量流教程都是从图像识别开始的。有没有人可以帮助我提供代码或一个如何在python中做到这一点的教程?提前谢谢。编辑-我计划使用的x1 x2坐标是1,1,y是2或4,6,y是10。我想为程序提供可供学习的数据。我试图从tensorflow网站上学习,但它似乎比我想要的要复杂得多。


Tags: 数据代码程序编辑网站tensorflow教程图像识别
2条回答

下面是一个小片段,让您开始:

import numpy as np
import tensorflow as tf

#a placeholder is like a variable that you can
#set later
a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
#build the sum operation
c = a+b
#get the tensorflow session
sess = tf.Session()

#initialize all variables
sess.run(tf.initialize_all_variables())

#Now you want to sum 2 numbers
#first set up a dictionary
#that includes the numbers
#The key of the dictionary
#matches the placeholders
# required for the sum operation
feed_dict = {a:2.0, b:3.0}

#now run the sum operation
ppx = sess.run([c], feed_dict)

#print the result
print(ppx)

首先,让我们从定义张量开始

import tensorflow as tf

a=tf.constant(7)
b=tf.constant(10)
c = tf.add(a,b)

现在,我们有一个简单的图,它可以添加两个常数。我们现在只需要创建一个会话来运行我们的图表:

simple_session = tf.Session()
value_of_c = simple_session.run(c)
print(value_of_c)   # 17
simple_session.close()

相关问题 更多 >