Basic addition in Tensorflow?

核能气质少年 提交于 2019-12-07 05:43:56

问题


I want to make a program where I enter in a set of x1 x2 and outputs a y. All of the tensor flow tutorials I can find start with image recognition. Can someone help me by providing me either code or a tutorial on how to do this in python? thanks in advance. edit- the x1 x2 coordinates I was planning to use would be like 1, 1 and the y would be 2 or 4, 6 and the y would be 10. I want to provide the program with data to learn from. I have tried to learn from the tensorflow website but it seemed way more complex that what I wanted.


回答1:


First, let's start with defining our Tensors

import tensorflow as tf

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

Now, we have our simple graph which can add two constants. All we need now is to create a session to run our graph:

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



回答2:


Here is a snippet to get you started:

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)


来源:https://stackoverflow.com/questions/39737507/basic-addition-in-tensorflow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!