Christopher Ell
Christopher Ell

Reputation: 2048

Exporting Outputs from a Neural Network

I have built a neural network in Tensorflow and would like to export the output into a text file. The neural network for each example has two outputs probabilities. I would like to export these two rows of data into a text file but am having trouble doing that because as I run it I need to put it into some type of object and then am unable to export any of these object: for example:

m = []

for (x, y) in test_dataset:
  logits = tf.nn.softmax(model(x))
  result_temp = np.asarray(logits)
  formatInt_temp = result_temp.astype(np.float)
  m.append(formatInt_temp)

txt_file = open('testing.txt', 'w')

txt_file.write(m)

The above code takes ndarrays and puts them into a list, originally I tried appending them to another ndarray but when I did that it gave me the error

AttributeError: 'numpy.ndarray' object has no attribute 'append'

A list was the only thing I could find I could export the ndarrays into. However once I have it in a list I can't export it because it must be exported as a string using the above method. If I then try putting that into a loop and exporting it one row at a time it says it can't export ndarrays like that. So does anyone know a better way of exporting the results of my model into a text file?

Thanks

Upvotes: 1

Views: 166

Answers (2)

Christopher Ell
Christopher Ell

Reputation: 2048

Thanks Alexander Liptak for showing me how to convert the list into ndarray using np.asarray() and export the array with np.savetxt(). But I also needed to reshape it as it was three dimensional because the original rows were split into batches of 32. So the final code I used was:

m = []

for (x, y) in test_dataset:
  logits = tf.nn.softmax(model(x))
  result_temp = np.asarray(logits, dtype=float)
  m.append(result_temp)

m = np.asarray(m)
m = np.reshape(m, (47168, 2))
np.savetxt("test.txt", m, delimiter=",")

Upvotes: 1

Alexander Liptak
Alexander Liptak

Reputation: 96

A useful function may be numpy.savetxt (see documentation):

This could be implemented into your code as follows:

for (x, y) in test_dataset:
    logits = tf.nn.softmax(model(x))
    result_temp = np.asarray(logits, dtype=float) #use this instead of astype
    np.savetxt('dataset_'+str(x)+'_'+str(y)+'.txt', results_temp)

You can also make use of the optional parameter delimeter in case you want your values comma separated instead of the default space separation.

If it is unhelpful to create many small files, and you would rather have one big text file, you can instead aggregate the numpy arrays into your list as you did in your code, then doing:

np.savetxt("data.txt", np.asarray(m))

Upvotes: 1

Related Questions