LoginSignup
1
0

More than 5 years have passed since last update.

tensorflowでfizzbuzz

Posted at

概要

tensorflowでfizzbuzzやってみた。

サンプルコード

import tensorflow as tf

l = 100
r = tf.Variable([str(i) for i in range(1, l + 1)], dtype = tf.string) 

def cond(i, _):
    return (tf.less(i, l + 1))

def body(i, _):
    e = tf.cond(tf.equal(tf.mod(i, 15), 0), lambda: tf.assign(r[i - 1], 'FizzBuzz'),
        lambda: tf.cond(tf.equal(tf.mod(i, 3), 0), lambda: tf.assign(r[i - 1], 'Fizz'),
            lambda: tf.cond(tf.equal(tf.mod(i, 5), 0), lambda: tf.assign(r[i - 1], 'Buzz'), lambda: r)))
    return (tf.add(i, 1), e)

_, g = tf.while_loop(cond, body, [1, r])

sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
print (g.eval())

以上。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0