import tensorflow as tf # c constant_node: constant node in the graph constant_node=tf.constant("Hi") # c sess_instance: Session instance sess_instance=tf.Session() # Run session, run constant_node out=sess_instance.run(constant_node) print(out) # b_placeholder'Hi'
constant_node_1=tf.constant(3.0,tf.float32) # tf.float32 is default constant_node_2=tf.constant(4.0) operation_node_1=tf.add(constant_node_1,constant_node_2) # ================================================================================ print("constant_node_1",constant_node_1) # constant_node_1: Tensor("Const_1:0",shape=(),dtype=float32) print("constant_node_2",constant_node_2) # constant_node_2: Tensor("Const_2:0",shape=(),dtype=float32) print("constant_node_3",constant_node_3) # operation_node_1: Tensor("Add:0",shape=(),dtype=float32) # ================================================================================ # You run tensorflow code in this way, by using Session object sess_instance=tf.Session() out_constant_node_1=sess_instance.run(constant_node_1) print(out_constant_node_1) # 3.0 out_constant_node_2=sess_instance.run(constant_node_2) print(out_constant_node_2) # 3.0 out_operation_node_1=sess_instance.run(operation_node_1) print(out_operation_node_1) # 7.0
a_placeholder=tf.placeholder(tf.float32) b_placeholder=tf.placeholder(tf.float32) add_op_node=a_placeholder+b_placeholder # ================================================================================ input_data={ a_placeholder:3, b_placeholder:4.5} out=sess_instance.run(add_op_node,feed_dict=input_data) print(out) # 7.5 # ================================================================================ intput_data2={ a_placeholder:[1,3], b_placeholder:[2,4]} sess_instance.run(add_op_node,feed_dict=intput_data2) # array([3., 7.], dtype=float32) # ================================================================================ # You create node add_then_3times_node=add_op_node*3 input_data3={ a_placeholder:3, b_placeholder:4.5} out=sess_instance.run(add_and_triple,feed_dict=input_data3) print(out) # 22.5