Skip to content

Commit

Permalink
restore Basic TensorFlow Examples
Browse files Browse the repository at this point in the history
  • Loading branch information
leejaymin committed Jan 31, 2017
1 parent 274bdc8 commit b9af59a
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 0 deletions.
23 changes: 23 additions & 0 deletions 0.2.Basic/Basic_usageOfConstant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import tensorflow as tf

# Create a Constant op that produces a 1x2 matrix. The op is
# added as a node to the default graph.
#
# The value returned by the constructor represents the output
# of the Constant op.
matrix1 = tf.constant([[3., 3.]])

# Create another Constant that produces a 2x1 matrix.
matrix2 = tf.constant([[2.],[2.]])

# Create a Matmul op that takes 'matrix1' and 'matrix2' as inputs.
# The returned value, 'product', represents the result of the matrix
# multiplication.
product = tf.matmul(matrix1, matrix2)

# Launch the default graph.
sess = tf.Session()

with tf.Session() as sess:
result = sess.run([product])
print(result)
12 changes: 12 additions & 0 deletions 0.2.Basic/Basic_usageOfPlaceholder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import tensorflow as tf


input1 = tf.placeholder(tf.float32)
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.]}))

# output:
# [array([ 14.], dtype=float32)]
32 changes: 32 additions & 0 deletions 0.2.Basic/Basic_usageOfVariables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import tensorflow as tf

# Create a Variable, that will be initialized to the scalar value 0.
state = tf.Variable(0, name="counter")

# Create an Op to add one to `state`.

one = tf.constant(1)
new_value = tf.add(state, one)
update = tf.assign(state, new_value)

# Variables must be initialized by running an `init` Op after having
# launched the graph. We first have to add the `init` Op to the graph.
init_op = tf.initialize_all_variables()

# Launch the graph and run the ops.
with tf.Session() as sess:
# Run the 'init' op
sess.run(init_op)
# Print the initial value of 'state'
print(sess.run(state))
# Run the op that updates 'state' and print 'state'.
for _ in range(3):
sess.run(update)
print(sess.run(state))

# output:

# 0
# 1
# 2
# 3
116 changes: 116 additions & 0 deletions 0.2.Basic/TensorFlow Basic of concept.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# TensorFlow 기본 개념 "
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"WARNING:tensorflow:From <ipython-input-36-265b04346152>:17 in <module>.: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.\nInstructions for updating:\nUse `tf.global_variables_initializer` instead.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"epoch 0, output: 0.800000011920929\nepoch 10, output: 0.47898948192596436\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"epoch 20, output: 0.2867887020111084\nepoch 30, output: 0.17171096801757812\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"epoch 40, output: 0.10280969738960266\nepoch 50, output: 0.06155595928430557\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"epoch 60, output: 0.036855824291706085\nepoch 70, output: 0.022066941484808922\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"epoch 80, output: 0.01321229338645935\nepoch 90, output: 0.007910688407719135\n"
]
}
],
"source": [
"import tensorflow as tf\n",
"x = tf.constant(1.0, name='input')\n",
"w = tf.Variable(0.8, name='weight')\n",
"y = tf.mul(w, x, name='output')\n",
"y_ = tf.constant(0.0, name='correct_value')\n",
"loss = tf.pow(y - y_, 2, name='loss')\n",
"train_step = tf.train.GradientDescentOptimizer(0.025).minimize(loss)\n",
"\n",
"for value in [x, w, y, y_, loss]:\n",
" tf.scalar_summary(value.op.name, value)\n",
"\n",
"summaries = tf.merge_all_summaries()\n",
"\n",
"sess = tf.Session()\n",
"summary_writer = tf.train.SummaryWriter('log_simple_stats', sess.graph)\n",
"\n",
"sess.run(tf.initialize_all_variables())\n",
"for i in range(100):\n",
" if i % 10 ==0:\n",
" print(\"epoch {}, output: {}\".format(i, sess.run(y)))\n",
" summary_writer.add_summary(sess.run(summaries), i)\n",
" sess.run(train_step)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
""
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2.0
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Empty file added 0.2.Basic/__init__.py
Empty file.
21 changes: 21 additions & 0 deletions 0.2.Basic/basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'''
Created on Nov 17, 2015
@author: root
'''

import tensorflow as tf

a = tf.placeholder(tf.int16)
b = tf.placeholder(tf.int16)

# Define some operations
add = tf.add(a, b)
mul = tf.mul(a, b)

with tf.Session() as sess:
print ("Addition with variables: %i" % sess.run(add, feed_dict={a:2, b:3}))
print ("Multiplication with variables: %d" % sess.run(mul, feed_dict={a:2, b:3}))



13 changes: 13 additions & 0 deletions 0.2.Basic/hellowTensorFlow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'''
Created on Nov 17, 2015
@author: root
'''
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print (sess.run(hello))

a = tf.constant(10)
b = tf.constant(32)
print (sess.run(a+b))

0 comments on commit b9af59a

Please sign in to comment.