0

I am trying to simulate Conway's Game of life in Tensorflow using python and have successfully implemented the code to simulate the game with a random initial 2d array using

tf.random_uniform(shape, minval=0, maxval=2, dtype=tf.int32)

Now I wish to initialize the 2d matrix with an array of 0s and 1s from a csv file having 0s and 1s in a 2d format. How should I initialize the initial Board for the Conway's game??

My code till now:

shape = (5,5)
initial_board = tf.random_uniform(shape, minval=0, maxval=2, dtype=tf.int32)
with tf.Session() as session:
    X = session.run(initial_board)

X is my starting 2d array.

1 Answers1

1

There are several ways, see below, for example:

import tensorflow as tf

initial_board = tf.constant([[1, 0, 0, 0, 0], [1, 1, 0, 0, 0], [1, 0, 1, 0, 0], [1, 0, 0, 0, 1]], dtype=tf.int32)
with tf.Session() as session:
    X = session.run(initial_board)
    print X
  • and If i want the values to load from external file, like a csv file? – Shubham Chowdhary Nov 23 '16 at 13:49
  • 1
    You can use numpy to load a file with 2d array from csv. See, for example, these answers: http://stackoverflow.com/questions/4315506/load-csv-into-2d-matrix-with-numpy-for-plotting, http://stackoverflow.com/questions/10937918/loading-a-file-into-a-numpy-array-with-python Once you have an array loaded to numpy's array, just pass it to tf.constant. – Gregory Begelman Nov 23 '16 at 13:54