Skip to content

Instantly share code, notes, and snippets.

@shinob
Last active July 12, 2017 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinob/99c0a9d512c934320b6554980ae32b70 to your computer and use it in GitHub Desktop.
Save shinob/99c0a9d512c934320b6554980ae32b70 to your computer and use it in GitHub Desktop.
RubyでTensorFlowを使う with PyCall ref: http://qiita.com/mix_dvd/items/ab46ac5e6bf48c45a4e5
$ gem install --pre pycall
$ gem install --pre matplotlib
y = w \times x + b
y = w \times x + b
$ ruby tf.rb
0 -0.075883001 0.61687899
20 0.031335868 0.34092095
40 0.082482144 0.31043994
60 0.095530763 0.30266351
80 0.098859809 0.30067953
100 0.099709123 0.30017337
120 0.099925794 0.30004424
140 0.099981084 0.30001128
160 0.099995196 0.30000287
180 0.099998787 0.30000073
200 0.099999689 0.30000019
$ ruby tf.rb
0 -0.075883001 0.61687899
20 0.031335868 0.34092095
40 0.082482144 0.31043994
60 0.095530763 0.30266351
80 0.098859809 0.30067953
100 0.099709123 0.30017337
120 0.099925794 0.30004424
140 0.099981084 0.30001128
160 0.099995196 0.30000287
180 0.099998787 0.30000073
200 0.099999689 0.30000019
# 略
# Fit the line.
w_log = []
b_log = []
(0..200).each do |step|
sess.run.(train)
#if step % 20 == 0 then
# puts "#{step}\t#{sess.run.(w)[0]}\t#{sess.run.(b)[0]}"
#end
w_log << sess.run.(w)[0]
b_log << sess.run.(b)[0]
end
# Learns best fit is W: [0.1], b: [0.3]
pyimport 'matplotlib.pyplot', as: 'plt'
require 'matplotlib/iruby'
Matplotlib::IRuby.activate
plt.plot.(w_log)
plt.plot.(b_log)
plt.show.()
require 'pycall/import'
include PyCall::Import
pyimport 'numpy', as: 'np'
pyimport 'tensorflow', as: 'tf'
# Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3
x_data = np.random.rand.(100).astype.(np.float32)
y_data = x_data * 0.1 + 0.3
# Try to find values for W and b that compute y_data = W * x_data + b
# (We know that W should be 0.1 and b 0.3, but Tensorflow will
# figure that out for us.)
w = tf.Variable.(tf.random_uniform.([1], -1.0, 1.0))
b = tf.Variable.(tf.zeros.([1]))
y = w * x_data + b
# Minimize the mean squared errors.
loss = tf.reduce_mean.(tf.square.(y - y_data))
optimizer = tf.train.GradientDescentOptimizer.(0.5)
train = optimizer.minimize.(loss)
# Before starting, initialize the variables. We will 'run' this first.
init = tf.initialize_all_variables.()
# Launch the graph.
sess = tf.Session.()
sess.run.(init)
# Fit the line.
(0..200).each do |step|
sess.run.(train)
if step % 20 == 0 then
puts "#{step}\t#{sess.run.(w)[0]}\t#{sess.run.(b)[0]}"
end
end
# Learns best fit is W: [0.1], b: [0.3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment