投稿

ラベル(tensorflow)が付いた投稿を表示しています

tensorflow で 自己符号化器

イメージ
MLPのChapter5の自己符号化器をTensorflowで試す。 誤差関数は二乗誤差、、、の平方根。 何故か平方根取らないと値が発散してしまった。。。 ・データ:MNIST ・重み共有:なし ・恒等写像 ・中間層:40ユニット ・ミニパッチ(100サンプル毎) import tensorflow as tf from PIL import Image import os def weight_variable (shape): initial = tf.truncated_normal(shape , stddev = 0.1 ) return tf.Variable(initial) def output_image (path , num_images , size , iimg , oimg , prefix= "" ): if len (iimg) > size[ 0 ]*size[ 1 ]*num_images: iimg = iimg[: size[ 0 ]*size[ 1 ]*num_images] if len (oimg) > size[ 0 ]*size[ 1 ]*num_images: oimg = oimg[: size[ 0 ]*size[ 1 ]*num_images] pdata = [] for i in range (size[ 1 ]*num_images): st = size[ 1 ]*i ed = size[ 1 ]*(i+ 1 ) pdata.extend(iimg[st:ed]) pdata.extend(oimg[st:ed]) im = Image.new( 'L' , (size[ 0 ]* 2 , size[ 1 ]*num_images)) im.putdata(pdata , 256 ) im.save(os.path.join(path , prefix + '.png' )) def main (): from tensorflow...

tensorflow で K-means法

イメージ
元ネタは FIRST CONTACT WITH TENSORFLOW という本で、参照元は K-Means.ipynb import tensorflow as tf import pandas as pd import matplotlib.pyplot as plt import seaborn as sns from numpy.random import normal def kmeans(num_points , k): vectors_set = [(normal( 0.0 , 0.9 ) , normal( 0.0 , 0.9 )) for _ in range(int(num_points/ 2 ))] vectors_set.extend((normal( 3.0 , 0.5 ) , normal( 1.0 , 0.5 )) for _ in range(int(num_points/ 2 ))) vectors= tf.constant(vectors_set) centroides = tf.Variable(tf.slice(tf.random_shuffle(vectors) , [ 0 , 0 ] , [k , - 1 ])) expanded_vectors = tf.expand_dims(vectors , 0 ) expanded_centroides = tf.expand_dims(centroides , 1 ) sub = tf.sub(expanded_vectors , expanded_centroides) square = tf.square(sub) sum = tf.reduce_sum(square , 2 ) assignments = tf.argmin(sum , 0 ) means = tf.concat( 0 , [tf.reduce_mean(tf.gather(vectors , tf.reshape(tf.where(tf.equal(assignments , c)) , [ 1 , - 1 ])) , re...

MNIST をpngで出力

通番とラベルをファイル名にしてpng形式で出力 from PIL import Image import os path = "MNIST_data/" from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets(path , one_hot= False ) batch = mnist.train.next_batch(mnist.train.num_examples) for i in range(mnist.train.num_examples): data = batch[ 0 ][i] label = batch[ 1 ][i] im = Image.new( 'L' , ( 28 , 28 )) im.putdata(data , 256 ) im.save(os.path.join(path , 'train_{0:05d}' .format(i) + '_{0:01d}' .format(label) + '.png' )) batch = mnist.test.next_batch(mnist.test.num_examples) for i in range(mnist.test.num_examples): data = batch[ 0 ][i] label = batch[ 1 ][i] im = Image.new( 'L' , ( 28 , 28 )) im.putdata(data , 256 ) im.save(os.path.join(path , 'test_{0:05d}' .format(i) + '_{0:01d}' .format(label) + '.png' ))

ミニマムなTensorflowコード

3*4を実行するだけ、、、 値の代入先にはplaceholder(計算実行時に値を格納する変数?)を利用。 import tensorflow as tf a = tf.placeholder( "float" ) b = tf.placeholder( "float" ) y = tf.mul(a , b) sess = tf.Session() print (sess.run(y , feed_dict ={a: 3 , b: 4 }))