0

I'm currently working on a neural network to play Rock-Paper-Scissors, but I've run into an enormous issue.

I'm having the neural network predict what will happen next based on a history of three moves, where with every move by the human, a new list is made in an array that contains the two previous moves and the new one. The neural network then trains and learns off of this. My code for that can be found below.

#add new situation, with what is currently happening to make current prediction with adjusted weights

current_turn = np.array([[input_data[len(input_data) - 1][1], input_data[len(input_data) - 1][2], output_data[len(output_data) - 1][0]]])
np.append(input_data, current_turn, axis = 0)

I'm using the Python system NumPy, and it is refusing to append these two arrays, such that the neural network isn't learning.

Edit: One of the responses recognized that one must reassign the array to this newly appended array. When I tried this later on, as shown below, it once again would not work.

if human_choice == "r":
        output_data = np.append(output_data, ([0]))
elif human_choice == "p":
        output_data = np.append(output_data, ([0.5]))
elif human_choice == "s":
        output_data = np.append(output_data, ([1]))

Is there a better way to join these arrays such that the algorithm can learn?

Note: The "append" isn't drawing any errors, yet seems to not do its job.

tdh
  • 884
  • 1
  • 11
  • 22
  • 1
    If you know the problem is to do with appending arrays, can you boil down your question into an example that's just about that? CBA to wade through large amounts of code that are highly specific to the neural-network/rock-paper-scissors context – jez Jan 04 '16 at 18:56
  • @jez Absolutely. I've made the change. – tdh Jan 04 '16 at 19:11
  • You are trying to use `append` as though it were a list method. But `np.append` is not a method, and does not work 'in-place'. It is just another way of calling `np.concatenate`. – hpaulj Jan 04 '16 at 20:06

2 Answers2

4

As the docs says,

Values are appended to a copy of this array.

(emphasis mine).

So np.append creates a new list instead of modification of your original one. You have to write:

input_data = np.append(input_data, current_turn, axis = 0)

Example:

import numpy as np
my_array = np.array([1, 2, 3])
print(my_array) 
# [1 2 3]
my_array = np.append(my_array, [4])
print(my_array)
# [1 2 3 4]

See also this question if are interested why np.append behaves in such a way.

Community
  • 1
  • 1
Ilya V. Schurov
  • 7,687
  • 2
  • 40
  • 78
  • See: http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.append.html – fgoettel Jan 04 '16 at 18:58
  • Thank you for the answer. When I did this, it didn't work on another append later in the program. I will add this to the question immediately. – tdh Jan 04 '16 at 19:07
  • I've made the edit. I would sincerely appreciate your input. – tdh Jan 04 '16 at 19:11
  • 1
    @tdh, could you please explain, what does it mean "it once again would not work"? What is hapenning and what should happen? – Ilya V. Schurov Jan 04 '16 at 19:11
  • @IlyaV.Schurov Of course. I'm sorry for not being clear. Appending the value to the array should be happening behind-the-scenes, but, to ensure it is working properly, I print it to the console. It seems that when I print it, it hasn't appended, although I ensure that I do the print after the action. – tdh Jan 04 '16 at 19:14
  • 1
    @tdh, that seems to be strange. I added an example to the answer which definitely works for me. Could you please reproduce your problem in a similar way (e.g. create MCVE, see http://stackoverflow.com/help/mcve)? Or provide more debug info. – Ilya V. Schurov Jan 04 '16 at 19:19
  • @IlyaV.Schurov It seems I was logging the result wrong. Thanks! – tdh Jan 04 '16 at 19:36
0

You can use numpy.concatenate method instead.

import numpy as np
arr = np.array([1, 2])

arr = np.concatenate((arr,[4]))
print(arr)
# [1 2 3 4]

see docs for more help: http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.concatenate.html

stackfy
  • 11
  • 5
  • I sort of wish they never created the `append` function. This is a typical example of its misuse. – hpaulj Jan 04 '16 at 20:00