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 Answer 1

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
Sign up to request clarification or add additional context in comments.

2 Comments

and If i want the values to load from external file, like a csv file?
You can use numpy to load a file with 2d array from csv. See, for example, these answers: stackoverflow.com/questions/4315506/…, stackoverflow.com/questions/10937918/… Once you have an array loaded to numpy's array, just pass it to tf.constant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.