0

I am working with tensorflow and numpy, and came across an issue where running a probability_model() on a single test example. My array, x_test is a 2d array.

probability_model(x_test[:1])

works properly, however

probability_model(x_test[0])

returns WARNING:tensorflow:Model was constructed with shape Tensor("sequential_input:0", shape=(None, 28, 28), dtype=float32) for input (None, 28, 28), but it was re-called on a Tensor with incompatible shape (28, 28).

I thought that x_test[0] and x_test[:1] would return the same 1D array, however it appears one results in a (None, 28, 28) dimensioned array, and the other results in a (28,28) array. So my question is not about tensorflow (I was just using it to show my error), do these expressions in fact result in different arrays, and if so what is the difference, because when analyzing their outputs they appear the same. Finally, is there an expression I can use to convert x_test[0] to the (None, 28, 28) size I need to call my function?

Thanks

figbar
  • 714
  • 12
  • 35

1 Answers1

2

A simple test shows that arr[0] returns the object in location in 0, and arr[:1] returns the list cut out at index 1 (so just the first object).

a = [1, 2, 3]
print(a[0], a[:1])

Outputs:

1 [1]
felipe
  • 7,324
  • 2
  • 28
  • 37
  • Interesting. I don't have a lot of experience with numpy sorry. So then how can I get a[0] in the format of [1] and not 1. – figbar Feb 19 '20 at 04:23
  • Or is this the best way to get this behavior. For example if i want to return [2], what is the best way to do this – figbar Feb 19 '20 at 04:25
  • Simply `[a[2]]` should be enough. The more verbose way would of course be `a[1:2]`. – felipe Feb 19 '20 at 04:29
  • those are not the same, a[:1] = array([0]), but [a[0]] = [array(0)], or something like that – figbar Feb 19 '20 at 12:57
  • never mind a[[0]] provided the output i needed – figbar Feb 19 '20 at 13:25
  • Oh, my mistake -- you are using `numpy.array`, not the built-in `list`. In such case, indeed `np.array(a[0])` would work. – felipe Feb 19 '20 at 15:29