Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: __enter__ from "with tf.Session as sess:"

So, this might be a dumb or obvious question but bear with me. I'm a mathematics student, and I'm in my final year and have been learning to work with neural nets for fun. I am no programmer so errors are something I encounter frequently. Usually I can sort them out, today however I received one I just can't figure out. When I attempt to execute my code I get an error saying:

"Traceback (most recent call last):
File "C:\Python Practice\gan.py", line 93, in <module>
n()
File "C:\Python Practice\gan.py", line 73, in nn
with tf.Session as sess:
AttributeError: __enter__

The code from line 72 until the end looks like:

def network_run():
with tf.Session as sess:
    sess.run(tf.global_variables_initializer())
    for i in range(200):
        sess.run(opt_D, feed_dict={x_ten: images[np.random.choice(range(len(images)), batch_size)].reshape(batch_size, x_ten_size), 
        z_ten:z_noise(batch_size)})
        sess.run(opt_G, feed_dict={z_ten:z_noise(batch_size)})
        sess.run(opt_G, feed_dict={z_ten:z_noise(batch_size)})

        gen_cost=sess.run(G_img, feed_dict={z_ten:z_noise(batch_size)})
        disc_cost=sess.run(D_img, feed_dict={x_ten: images[np.random.choice(range(len(images)), batch_size)].reshape(batch_size, x_ten_size), 
        z_ten:z_noise(batch_size)})

        image=sess.run(G(z_ten), feed_dict={z_ten:z_noise(batch_size)})
        df=sess.run(tf.sigmoid(D_img_fake), feed_dict={z_ten:z_noise()})
        print (i, gen_cost, disc_cost, image.max(), df[0][0])

    image=sess.run(G(z_ten), feed_dict={z_ten:z_noise(batch_size)})
    image1 = image[0].reshape([28, 28])
    im = Image.fromarray(image1)
    im.show()
  network_run()

Thanks in advance to anyone who helps this blubbering fool out- Max

like image 220
MaxDoesMath Avatar asked Dec 14 '17 00:12

MaxDoesMath


1 Answers

This looks like a simple typo. The following line in your code:

with tf.Session as sess:

...should have parentheses after Session, like so:

with tf.Session() as sess:
like image 174
mrry Avatar answered Sep 23 '22 10:09

mrry