<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Tensor Flow to create a useless images
To learn how to encode a simple image and a GIF
Import needed for Tensorflow
Step1:  Import needed for Jupiter
Step2:  A function to save a picture
Step4:  A function to draw the cost function in Jupyter
Step5:  Create some random pictures
Encode the input (a number)
This example convert the number to a binary representation
Step6:  To create a GIF
<ASSISTANT_TASK:>
Python Code:
import numpy as np
import tensorflow as tf
Explanation: Tensor Flow to create a useless images
To learn how to encode a simple image and a GIF
Import needed for Tensorflow
End of explanation
%matplotlib notebook
import matplotlib
import matplotlib.pyplot as plt
from IPython.display import Image
Explanation: Import needed for Jupiter
End of explanation
def write_png(tensor, name):
    casted_to_uint8 = tf.cast(tensor, tf.uint8)
    converted_to_png = tf.image.encode_png(casted_to_uint8)
    f = open(name, "wb+")
    f.write(converted_to_png.eval())
    f.close() 
Explanation: A function to save a picture
End of explanation
class CostTrace:
    A simple example class
    def __init__(self):
        self.cost_array = []
    def log(self, cost):
        self.cost_array.append(cost)
    def draw(self):
        plt.figure(figsize=(12,5))
        plt.plot(range(len(self.cost_array)), self.cost_array, label='cost')
        plt.legend()
        plt.yscale('log')
        plt.show()
Explanation: A function to draw the cost function in Jupyter
End of explanation
# Init size
width = 100
height = 100
RGB = 3
shape = [height,width, RGB]
# Create the generated tensor as a variable
rand_uniform = tf.random_uniform(shape, minval=0, maxval=255, dtype=tf.float32)
generated = tf.Variable(rand_uniform)
#define the cost function
c_mean = tf.reduce_mean(tf.pow(generated,2)) # we want a low mean
c_max = tf.reduce_max(generated) # we want a low max
c_min = -tf.reduce_min(generated) # we want a high mix
c_diff = 0
for i in range(0,height-1, 1):
    line1 = tf.gather(generated, i,)
    line2 = tf.gather(generated, i+1)
    c_diff += tf.reduce_mean(tf.pow(line1-line2-30, 2)) # to force a gradient
cost = c_mean + c_max + c_min + c_diff
#cost = c_mean + c_diff
print ('cost defined')
train_op = tf.train.GradientDescentOptimizer(0.5).minimize(cost, var_list=[generated])
print ('train_op defined')
# Initializing the variables
init = tf.initialize_all_variables()
print ('variables initialiazed defined')    
# Launch the graph
with tf.Session() as sess:
    sess.run(init)
    print ('init done')
    cost_trace = CostTrace()
    for epoch in range(0,10000):
        sess.run(train_op)
        if (epoch % 100 == 0):
            c = cost.eval()
            print ('epoch', epoch,'cost' ,c, c_mean.eval(), c_min.eval(), c_max.eval(), c_diff.eval())
            cost_trace.log(c)
            write_png(generated, "generated{:06}.png".format(epoch))
print ('all done') 
cost_trace.draw()
Image("generated000000.png")
Explanation: Create some random pictures
Encode the input (a number)
This example convert the number to a binary representation
End of explanation
from PIL import Image, ImageSequence
import glob, sys, os
os.chdir(".")
frames = []
for file in glob.glob("gene*.png"):
    print(file)
    im = Image.open(file)
    frames.append(im)
from images2gif import writeGif
writeGif("generated.gif", frames, duration=0.1)
Explanation: To create a GIF
End of explanation
<END_TASK>