0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

wslのtensorflowでkerasのbackend その2

Posted at

概要

wslのtensorflowでkerasのbackendを試す。
練習問題、やってみた。

練習問題

fizzbuzzを書け。

サンプルコード

from tensorflow.python.keras import backend as K

kvar = K.arange(1, 100, step = 1, dtype = 'int32')
fizzbuzz = K.constant("FizzBuzz", dtype = tf.string)
buzz = K.constant("Buzz", dtype = tf.string)
fizz = K.constant("Fizz", dtype = tf.string)

def func(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))))

map = K.map_fn(func, kvar, name = None, dtype = tf.string)
print (K.eval(map))

実行結果

[b'1' b'2' b'Fizz' b'4' b'Buzz' b'Fizz' b'7' b'8' b'Fizz' b'Buzz' b'11'
 b'Fizz' b'13' b'14' b'FizzBuzz' b'16' b'17' b'Fizz' b'19' b'Buzz' b'Fizz'
 b'22' b'23' b'Fizz' b'Buzz' b'26' b'Fizz' b'28' b'29' b'FizzBuzz' b'31'
 b'32' b'Fizz' b'34' b'Buzz' b'Fizz' b'37' b'38' b'Fizz' b'Buzz' b'41'
 b'Fizz' b'43' b'44' b'FizzBuzz' b'46' b'47' b'Fizz' b'49' b'Buzz' b'Fizz'
 b'52' b'53' b'Fizz' b'Buzz' b'56' b'Fizz' b'58' b'59' b'FizzBuzz' b'61'
 b'62' b'Fizz' b'64' b'Buzz' b'Fizz' b'67' b'68' b'Fizz' b'Buzz' b'71'
 b'Fizz' b'73' b'74' b'FizzBuzz' b'76' b'77' b'Fizz' b'79' b'Buzz' b'Fizz'
 b'82' b'83' b'Fizz' b'Buzz' b'86' b'Fizz' b'88' b'89' b'FizzBuzz' b'91'
 b'92' b'Fizz' b'94' b'Buzz' b'Fizz' b'97' b'98' b'Fizz']

以上。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?