import tensorflow as tf x = tf.constant(100, name='x') y = tf.Variable(x + 50, name='y') print(y)
WARNING:tensorflow:From /databricks/python/local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
<tf.Variable 'y:0' shape=() dtype=int32_ref>
# all nodes get added to default graph (unless we specify otherwise) # we can reset the default graph -- so it's not cluttered up: tf.reset_default_graph() x = tf.constant(data, name='x') y = tf.Variable(x * 10, name='y') init_node = tf.global_variables_initializer() with tf.Session() as session: session.run(init_node) print(session.run(y))
[[111.86529181 125.81984612 119.18791344]
[134.69655487 115.82286357 126.27472191]
[122.50746386 118.49474186 112.810347 ]]
with tf.Session() as session: for i in range(3): x = x + 1 print(session.run(x)) print("----------------------------------------------")
[[12.18652918 13.58198461 12.91879134]
[14.46965549 12.58228636 13.62747219]
[13.25074639 12.84947419 12.2810347 ]]
----------------------------------------------
[[13.18652918 14.58198461 13.91879134]
[15.46965549 13.58228636 14.62747219]
[14.25074639 13.84947419 13.2810347 ]]
----------------------------------------------
[[14.18652918 15.58198461 14.91879134]
[16.46965549 14.58228636 15.62747219]
[15.25074639 14.84947419 14.2810347 ]]
----------------------------------------------
x = tf.placeholder("float") y = tf.placeholder("float") m = tf.Variable([1.0], name="m-slope-coefficient") # initial values ... for now they don't matter much b = tf.Variable([1.0], name="b-intercept") y_model = tf.multiply(x, m) + b error = tf.square(y - y_model) train_op = tf.train.GradientDescentOptimizer(0.01).minimize(error) model = tf.global_variables_initializer() with tf.Session() as session: session.run(model) for i in range(10): x_value = np.random.rand() y_value = x_value * 2 + 6 # we know these params, but we're making TF learn them session.run(train_op, feed_dict={x: x_value, y: y_value}) out = session.run([m, b]) print(out) print("Model: {r:.3f}x + {s:.3f}".format(r=out[0][0], s=out[1][0]))
[array([1.4387712], dtype=float32), array([1.9758162], dtype=float32)]
Model: 1.439x + 1.976
SDS-2.x, Scalable Data Engineering Science
This is a 2019 augmentation and update of Adam Breindel's initial notebooks.
Last refresh: Never