LoginSignup
0
0

More than 5 years have passed since last update.

kerasでbackend その5

Posted at

概要

kerasでbackendで、fizzbuzz書いてみた。

サンプルコード

from tensorflow.contrib.keras.python.keras import backend as K
import tensorflow as tf

fizz = K.constant("Fizz", dtype = tf.string)
buzz = K.constant("Buzz", dtype = tf.string)
fizzbuzz = K.constant("FizzBuzz", dtype = tf.string)

def fn(x):
    return K.switch(tf.equal(tf.mod(x, 15), 0), fizzbuzz, K.switch(tf.equal(tf.mod(x, 5), 0), buzz, K.switch(tf.equal(tf.mod(x, 3), 0), fizz, tf.as_string(x))))

kvar = K.arange(1, 100, step = 1, dtype = 'int32')
map = K.map_fn(fn, kvar, name = None, dtype = tf.string)
print (K.eval(map))

以上。

0
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
0
0