Keras Cant Seem To Configure inputShape












0















I'm trying to do the "Hello World" of Keras with Iris-Flowers and I cannot configure the input_shape. This is my error message:



"ValueError: Error when checking input: expected dense_1_input to have 3 dimensions, but got array with shape (120, 4)"


When I change my input_shape to the received input shape, it just changes what it says the received input is. As you know there are 4 inputs and one output with this data set. Does anyone know how I would configure my input_shape?



Here's my code



import tensorflow as tf

text_file = open("iris.data.txt")
rawData = text_file.read().split('n')
text_file.close()

for x in range(0,150):
rawData[x] = rawData[x].split(',')

xs_train =
ys_train =
for i in range (0,40):
ys_train.append(rawData[i][4])
xs_train.append([rawData[i][0], rawData[i][1], rawData[i][2], rawData[i][3]])
for i in range (50,90):
ys_train.append(rawData[i][4])
xs_train.append([rawData[i][0], rawData[i][1], rawData[i][2], rawData[i][3]])
for i in range (100,140):
ys_train.append(rawData[i][4])
xs_train.append([rawData[i][0], rawData[i][1], rawData[i][2], rawData[i][3]])

xs_test =
ys_test =
for i in range (40,50):
ys_test.append(rawData[i][4])
xs_test.append([rawData[i][0], rawData[i][1], rawData[i][2], rawData[i][3]])
for i in range (90,100):
ys_test.append(rawData[i][4])
xs_test.append([rawData[i][0], rawData[i][1], rawData[i][2], rawData[i][3]])
for i in range (140,150):
ys_test.append(rawData[i][4])
xs_test.append([rawData[i][0], rawData[i][1], rawData[i][2], rawData[i][3]])

# print(xs_train)

for i in range(0, len(ys_train)):
if ys_train[i] == "Iris-setosa":
ys_train[i] = [1,0,0]
if ys_train[i] == "Iris-versicolor":
ys_train[i] = [0,1,0]
if ys_train[i] == "Iris-virginica":
ys_train[i] = [0,0,1]

# print(ys_train)


model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(4, input_shape=(1,4), activation= 'relu'))
model.add(tf.keras.layers.Dense(3, activation=tf.nn.softmax))
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(xs_train, ys_train, epochs=3)


My inputs are formatted so that each array is a dataset, each set including 4
data points, it goes as so:



[['5.1', '3.5', '1.4', '0.2'],
['4.9', '3.0', '1.4', '0.2'],
['4.7', '3.2', '1.3', '0.2']...]









share|improve this question



























    0















    I'm trying to do the "Hello World" of Keras with Iris-Flowers and I cannot configure the input_shape. This is my error message:



    "ValueError: Error when checking input: expected dense_1_input to have 3 dimensions, but got array with shape (120, 4)"


    When I change my input_shape to the received input shape, it just changes what it says the received input is. As you know there are 4 inputs and one output with this data set. Does anyone know how I would configure my input_shape?



    Here's my code



    import tensorflow as tf

    text_file = open("iris.data.txt")
    rawData = text_file.read().split('n')
    text_file.close()

    for x in range(0,150):
    rawData[x] = rawData[x].split(',')

    xs_train =
    ys_train =
    for i in range (0,40):
    ys_train.append(rawData[i][4])
    xs_train.append([rawData[i][0], rawData[i][1], rawData[i][2], rawData[i][3]])
    for i in range (50,90):
    ys_train.append(rawData[i][4])
    xs_train.append([rawData[i][0], rawData[i][1], rawData[i][2], rawData[i][3]])
    for i in range (100,140):
    ys_train.append(rawData[i][4])
    xs_train.append([rawData[i][0], rawData[i][1], rawData[i][2], rawData[i][3]])

    xs_test =
    ys_test =
    for i in range (40,50):
    ys_test.append(rawData[i][4])
    xs_test.append([rawData[i][0], rawData[i][1], rawData[i][2], rawData[i][3]])
    for i in range (90,100):
    ys_test.append(rawData[i][4])
    xs_test.append([rawData[i][0], rawData[i][1], rawData[i][2], rawData[i][3]])
    for i in range (140,150):
    ys_test.append(rawData[i][4])
    xs_test.append([rawData[i][0], rawData[i][1], rawData[i][2], rawData[i][3]])

    # print(xs_train)

    for i in range(0, len(ys_train)):
    if ys_train[i] == "Iris-setosa":
    ys_train[i] = [1,0,0]
    if ys_train[i] == "Iris-versicolor":
    ys_train[i] = [0,1,0]
    if ys_train[i] == "Iris-virginica":
    ys_train[i] = [0,0,1]

    # print(ys_train)


    model = tf.keras.models.Sequential()
    model.add(tf.keras.layers.Dense(4, input_shape=(1,4), activation= 'relu'))
    model.add(tf.keras.layers.Dense(3, activation=tf.nn.softmax))
    model.compile(optimizer='adam',
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy'])
    model.fit(xs_train, ys_train, epochs=3)


    My inputs are formatted so that each array is a dataset, each set including 4
    data points, it goes as so:



    [['5.1', '3.5', '1.4', '0.2'],
    ['4.9', '3.0', '1.4', '0.2'],
    ['4.7', '3.2', '1.3', '0.2']...]









    share|improve this question

























      0












      0








      0








      I'm trying to do the "Hello World" of Keras with Iris-Flowers and I cannot configure the input_shape. This is my error message:



      "ValueError: Error when checking input: expected dense_1_input to have 3 dimensions, but got array with shape (120, 4)"


      When I change my input_shape to the received input shape, it just changes what it says the received input is. As you know there are 4 inputs and one output with this data set. Does anyone know how I would configure my input_shape?



      Here's my code



      import tensorflow as tf

      text_file = open("iris.data.txt")
      rawData = text_file.read().split('n')
      text_file.close()

      for x in range(0,150):
      rawData[x] = rawData[x].split(',')

      xs_train =
      ys_train =
      for i in range (0,40):
      ys_train.append(rawData[i][4])
      xs_train.append([rawData[i][0], rawData[i][1], rawData[i][2], rawData[i][3]])
      for i in range (50,90):
      ys_train.append(rawData[i][4])
      xs_train.append([rawData[i][0], rawData[i][1], rawData[i][2], rawData[i][3]])
      for i in range (100,140):
      ys_train.append(rawData[i][4])
      xs_train.append([rawData[i][0], rawData[i][1], rawData[i][2], rawData[i][3]])

      xs_test =
      ys_test =
      for i in range (40,50):
      ys_test.append(rawData[i][4])
      xs_test.append([rawData[i][0], rawData[i][1], rawData[i][2], rawData[i][3]])
      for i in range (90,100):
      ys_test.append(rawData[i][4])
      xs_test.append([rawData[i][0], rawData[i][1], rawData[i][2], rawData[i][3]])
      for i in range (140,150):
      ys_test.append(rawData[i][4])
      xs_test.append([rawData[i][0], rawData[i][1], rawData[i][2], rawData[i][3]])

      # print(xs_train)

      for i in range(0, len(ys_train)):
      if ys_train[i] == "Iris-setosa":
      ys_train[i] = [1,0,0]
      if ys_train[i] == "Iris-versicolor":
      ys_train[i] = [0,1,0]
      if ys_train[i] == "Iris-virginica":
      ys_train[i] = [0,0,1]

      # print(ys_train)


      model = tf.keras.models.Sequential()
      model.add(tf.keras.layers.Dense(4, input_shape=(1,4), activation= 'relu'))
      model.add(tf.keras.layers.Dense(3, activation=tf.nn.softmax))
      model.compile(optimizer='adam',
      loss='sparse_categorical_crossentropy',
      metrics=['accuracy'])
      model.fit(xs_train, ys_train, epochs=3)


      My inputs are formatted so that each array is a dataset, each set including 4
      data points, it goes as so:



      [['5.1', '3.5', '1.4', '0.2'],
      ['4.9', '3.0', '1.4', '0.2'],
      ['4.7', '3.2', '1.3', '0.2']...]









      share|improve this question














      I'm trying to do the "Hello World" of Keras with Iris-Flowers and I cannot configure the input_shape. This is my error message:



      "ValueError: Error when checking input: expected dense_1_input to have 3 dimensions, but got array with shape (120, 4)"


      When I change my input_shape to the received input shape, it just changes what it says the received input is. As you know there are 4 inputs and one output with this data set. Does anyone know how I would configure my input_shape?



      Here's my code



      import tensorflow as tf

      text_file = open("iris.data.txt")
      rawData = text_file.read().split('n')
      text_file.close()

      for x in range(0,150):
      rawData[x] = rawData[x].split(',')

      xs_train =
      ys_train =
      for i in range (0,40):
      ys_train.append(rawData[i][4])
      xs_train.append([rawData[i][0], rawData[i][1], rawData[i][2], rawData[i][3]])
      for i in range (50,90):
      ys_train.append(rawData[i][4])
      xs_train.append([rawData[i][0], rawData[i][1], rawData[i][2], rawData[i][3]])
      for i in range (100,140):
      ys_train.append(rawData[i][4])
      xs_train.append([rawData[i][0], rawData[i][1], rawData[i][2], rawData[i][3]])

      xs_test =
      ys_test =
      for i in range (40,50):
      ys_test.append(rawData[i][4])
      xs_test.append([rawData[i][0], rawData[i][1], rawData[i][2], rawData[i][3]])
      for i in range (90,100):
      ys_test.append(rawData[i][4])
      xs_test.append([rawData[i][0], rawData[i][1], rawData[i][2], rawData[i][3]])
      for i in range (140,150):
      ys_test.append(rawData[i][4])
      xs_test.append([rawData[i][0], rawData[i][1], rawData[i][2], rawData[i][3]])

      # print(xs_train)

      for i in range(0, len(ys_train)):
      if ys_train[i] == "Iris-setosa":
      ys_train[i] = [1,0,0]
      if ys_train[i] == "Iris-versicolor":
      ys_train[i] = [0,1,0]
      if ys_train[i] == "Iris-virginica":
      ys_train[i] = [0,0,1]

      # print(ys_train)


      model = tf.keras.models.Sequential()
      model.add(tf.keras.layers.Dense(4, input_shape=(1,4), activation= 'relu'))
      model.add(tf.keras.layers.Dense(3, activation=tf.nn.softmax))
      model.compile(optimizer='adam',
      loss='sparse_categorical_crossentropy',
      metrics=['accuracy'])
      model.fit(xs_train, ys_train, epochs=3)


      My inputs are formatted so that each array is a dataset, each set including 4
      data points, it goes as so:



      [['5.1', '3.5', '1.4', '0.2'],
      ['4.9', '3.0', '1.4', '0.2'],
      ['4.7', '3.2', '1.3', '0.2']...]






      python tensorflow machine-learning keras






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 28 '18 at 23:24









      tanndlintanndlin

      104




      104
























          1 Answer
          1






          active

          oldest

          votes


















          0














          input_shape=(4,)


          Input shape doesn't care for the batch size, only for "each" sample size.






          share|improve this answer
























          • Nope, when I change input shape to (4,) it give me the error "ValueError: Error when checking target: expected dense_2 to have shape (None, 1) but got array with shape (120, 3)" Which is what confuses my, it says that the array shape has changed

            – tanndlin
            Nov 29 '18 at 19:12













          • You changed your last Dense layer. In the question, it's Dense(3..., now you're using Dense(1.... or another layer with size 1.

            – Daniel Möller
            Nov 29 '18 at 19:14











          • model.add(tf.keras.layers.Dense(4, input_shape=(4,), activation= 'relu')) This is my line of code... hmmmmm

            – tanndlin
            Nov 29 '18 at 19:15











          • "Target" is the output, not the input.

            – Daniel Möller
            Nov 29 '18 at 19:16











          • Ohhhh... This is related to the output, the configuration of the input is fine now. Ok, i think i can get this now

            – tanndlin
            Nov 29 '18 at 19:21












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53529614%2fkeras-cant-seem-to-configure-inputshape%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          input_shape=(4,)


          Input shape doesn't care for the batch size, only for "each" sample size.






          share|improve this answer
























          • Nope, when I change input shape to (4,) it give me the error "ValueError: Error when checking target: expected dense_2 to have shape (None, 1) but got array with shape (120, 3)" Which is what confuses my, it says that the array shape has changed

            – tanndlin
            Nov 29 '18 at 19:12













          • You changed your last Dense layer. In the question, it's Dense(3..., now you're using Dense(1.... or another layer with size 1.

            – Daniel Möller
            Nov 29 '18 at 19:14











          • model.add(tf.keras.layers.Dense(4, input_shape=(4,), activation= 'relu')) This is my line of code... hmmmmm

            – tanndlin
            Nov 29 '18 at 19:15











          • "Target" is the output, not the input.

            – Daniel Möller
            Nov 29 '18 at 19:16











          • Ohhhh... This is related to the output, the configuration of the input is fine now. Ok, i think i can get this now

            – tanndlin
            Nov 29 '18 at 19:21
















          0














          input_shape=(4,)


          Input shape doesn't care for the batch size, only for "each" sample size.






          share|improve this answer
























          • Nope, when I change input shape to (4,) it give me the error "ValueError: Error when checking target: expected dense_2 to have shape (None, 1) but got array with shape (120, 3)" Which is what confuses my, it says that the array shape has changed

            – tanndlin
            Nov 29 '18 at 19:12













          • You changed your last Dense layer. In the question, it's Dense(3..., now you're using Dense(1.... or another layer with size 1.

            – Daniel Möller
            Nov 29 '18 at 19:14











          • model.add(tf.keras.layers.Dense(4, input_shape=(4,), activation= 'relu')) This is my line of code... hmmmmm

            – tanndlin
            Nov 29 '18 at 19:15











          • "Target" is the output, not the input.

            – Daniel Möller
            Nov 29 '18 at 19:16











          • Ohhhh... This is related to the output, the configuration of the input is fine now. Ok, i think i can get this now

            – tanndlin
            Nov 29 '18 at 19:21














          0












          0








          0







          input_shape=(4,)


          Input shape doesn't care for the batch size, only for "each" sample size.






          share|improve this answer













          input_shape=(4,)


          Input shape doesn't care for the batch size, only for "each" sample size.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 29 '18 at 5:37









          Daniel MöllerDaniel Möller

          37.8k671108




          37.8k671108













          • Nope, when I change input shape to (4,) it give me the error "ValueError: Error when checking target: expected dense_2 to have shape (None, 1) but got array with shape (120, 3)" Which is what confuses my, it says that the array shape has changed

            – tanndlin
            Nov 29 '18 at 19:12













          • You changed your last Dense layer. In the question, it's Dense(3..., now you're using Dense(1.... or another layer with size 1.

            – Daniel Möller
            Nov 29 '18 at 19:14











          • model.add(tf.keras.layers.Dense(4, input_shape=(4,), activation= 'relu')) This is my line of code... hmmmmm

            – tanndlin
            Nov 29 '18 at 19:15











          • "Target" is the output, not the input.

            – Daniel Möller
            Nov 29 '18 at 19:16











          • Ohhhh... This is related to the output, the configuration of the input is fine now. Ok, i think i can get this now

            – tanndlin
            Nov 29 '18 at 19:21



















          • Nope, when I change input shape to (4,) it give me the error "ValueError: Error when checking target: expected dense_2 to have shape (None, 1) but got array with shape (120, 3)" Which is what confuses my, it says that the array shape has changed

            – tanndlin
            Nov 29 '18 at 19:12













          • You changed your last Dense layer. In the question, it's Dense(3..., now you're using Dense(1.... or another layer with size 1.

            – Daniel Möller
            Nov 29 '18 at 19:14











          • model.add(tf.keras.layers.Dense(4, input_shape=(4,), activation= 'relu')) This is my line of code... hmmmmm

            – tanndlin
            Nov 29 '18 at 19:15











          • "Target" is the output, not the input.

            – Daniel Möller
            Nov 29 '18 at 19:16











          • Ohhhh... This is related to the output, the configuration of the input is fine now. Ok, i think i can get this now

            – tanndlin
            Nov 29 '18 at 19:21

















          Nope, when I change input shape to (4,) it give me the error "ValueError: Error when checking target: expected dense_2 to have shape (None, 1) but got array with shape (120, 3)" Which is what confuses my, it says that the array shape has changed

          – tanndlin
          Nov 29 '18 at 19:12







          Nope, when I change input shape to (4,) it give me the error "ValueError: Error when checking target: expected dense_2 to have shape (None, 1) but got array with shape (120, 3)" Which is what confuses my, it says that the array shape has changed

          – tanndlin
          Nov 29 '18 at 19:12















          You changed your last Dense layer. In the question, it's Dense(3..., now you're using Dense(1.... or another layer with size 1.

          – Daniel Möller
          Nov 29 '18 at 19:14





          You changed your last Dense layer. In the question, it's Dense(3..., now you're using Dense(1.... or another layer with size 1.

          – Daniel Möller
          Nov 29 '18 at 19:14













          model.add(tf.keras.layers.Dense(4, input_shape=(4,), activation= 'relu')) This is my line of code... hmmmmm

          – tanndlin
          Nov 29 '18 at 19:15





          model.add(tf.keras.layers.Dense(4, input_shape=(4,), activation= 'relu')) This is my line of code... hmmmmm

          – tanndlin
          Nov 29 '18 at 19:15













          "Target" is the output, not the input.

          – Daniel Möller
          Nov 29 '18 at 19:16





          "Target" is the output, not the input.

          – Daniel Möller
          Nov 29 '18 at 19:16













          Ohhhh... This is related to the output, the configuration of the input is fine now. Ok, i think i can get this now

          – tanndlin
          Nov 29 '18 at 19:21





          Ohhhh... This is related to the output, the configuration of the input is fine now. Ok, i think i can get this now

          – tanndlin
          Nov 29 '18 at 19:21




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53529614%2fkeras-cant-seem-to-configure-inputshape%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)