Want to split train and test data gotten from a csv with tensorflowTensorFlow: Neural Network accuracy always 100% on train and test setsHow to use numpy array inputs in tensorflow RNNPythonshell.parser error using npm python-shellTraining on Patches of VIDEO FRAMES and Quality ScoresTensorflow Keras LSTM not training - affected by number_of_epochs, optimizer adamLSTM Nerual Network Input/Output dimensions errorKeras batch_size problem when using model.fit in custom layersAttributeError: 'Sequential' object has no attribute 'total_loss'Trying to create a fully connected neural network for CIFAR-10

Why does the 6502 have the BIT instruction?

Can a Beholder use rays in melee range?

How to prevent bad sectors?

Different PCB color ( is it different material? )

How to return && object from function?

Word Mastermind

Do firearms count as ranged weapons?

Why colon to denote that a value belongs to a type?

What are the problems in teaching guitar via Skype?

Why does the UK have more political parties than the US?

1960s sci-fi novella with a character who is treated as invisible by being ignored

Where do I put diamond mines on my map?

If a massive object like Jupiter flew past the Earth how close would it need to come to pull people off of the surface?

Do you play the upbeat when beginning to play a series of notes, and then after?

Is there any use case for the bottom type as a function parameter type?

Looking after a wayward brother in mother's will

Can non-English-speaking characters use wordplay specific to English?

Tic-Tac-Toe for the terminal

Is this story about US tax office reasonable?

Crossing US border with music files I'm legally allowed to possess

The Passive Wisdom (Perception) score of my character on D&D Beyond seems too high

Windows 10 Programs start without visual Interface

What F1 in name of seeds/varieties means?

Employer demanding to see degree after poor code review



Want to split train and test data gotten from a csv with tensorflow


TensorFlow: Neural Network accuracy always 100% on train and test setsHow to use numpy array inputs in tensorflow RNNPythonshell.parser error using npm python-shellTraining on Patches of VIDEO FRAMES and Quality ScoresTensorflow Keras LSTM not training - affected by number_of_epochs, optimizer adamLSTM Nerual Network Input/Output dimensions errorKeras batch_size problem when using model.fit in custom layersAttributeError: 'Sequential' object has no attribute 'total_loss'Trying to create a fully connected neural network for CIFAR-10






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I wanted to split train and test data of a csv with tensorflow but I didn't find an order like np.loadtxt in tensor and tried to do splits with numpy and convert it to tensor, but I get an error like below:



 TypeError: object of type 'Tensor' has no len()


and here is my code:



 import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split

x = tf.convert_to_tensor( np.loadtxt('data.csv', delimiter=','))
y = tf.convert_to_tensor(np.loadtxt('labels.csv', delimiter=','))

x_train, x_test, y_train, y_test = train_test_split(x, y,
test_size=0.25, random_state='')

model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape= (426,30,1)),
tf.keras.layers.Dense(126, activation=tf.nn.tanh),
#tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation=tf.nn.tanh)
])

model.compile(optimizer='sgd',
loss='mean_squared_error',
metrics=['accuracy'])

history = model.fit(x_train, y_train, epochs=5 ) #validation_data
= [x_test, y_test])
model.evaluate(x_test, y_test)

t_predicted = model.predict(x_test)
out_predicted = np.argmax(t_predicted, axis=1)
conf_matrix = tf.confusion_matrix(y_test, out_predicted)
with tf.Session():
print('Confusion Matrix: nn', tf.Tensor.eval(conf_matrix,
feed_dict=None, session=None))

# summarize history for accuracy
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()









share|improve this question




























    0















    I wanted to split train and test data of a csv with tensorflow but I didn't find an order like np.loadtxt in tensor and tried to do splits with numpy and convert it to tensor, but I get an error like below:



     TypeError: object of type 'Tensor' has no len()


    and here is my code:



     import tensorflow as tf
    import numpy as np
    import matplotlib.pyplot as plt
    from sklearn.model_selection import train_test_split

    x = tf.convert_to_tensor( np.loadtxt('data.csv', delimiter=','))
    y = tf.convert_to_tensor(np.loadtxt('labels.csv', delimiter=','))

    x_train, x_test, y_train, y_test = train_test_split(x, y,
    test_size=0.25, random_state='')

    model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape= (426,30,1)),
    tf.keras.layers.Dense(126, activation=tf.nn.tanh),
    #tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(10, activation=tf.nn.tanh)
    ])

    model.compile(optimizer='sgd',
    loss='mean_squared_error',
    metrics=['accuracy'])

    history = model.fit(x_train, y_train, epochs=5 ) #validation_data
    = [x_test, y_test])
    model.evaluate(x_test, y_test)

    t_predicted = model.predict(x_test)
    out_predicted = np.argmax(t_predicted, axis=1)
    conf_matrix = tf.confusion_matrix(y_test, out_predicted)
    with tf.Session():
    print('Confusion Matrix: nn', tf.Tensor.eval(conf_matrix,
    feed_dict=None, session=None))

    # summarize history for accuracy
    plt.plot(history.history['acc'])
    plt.plot(history.history['val_acc'])
    plt.title('model accuracy')
    plt.ylabel('accuracy')
    plt.xlabel('epoch')
    plt.legend(['train', 'test'], loc='upper left')
    plt.show()

    # summarize history for loss
    plt.plot(history.history['loss'])
    plt.plot(history.history['val_loss'])
    plt.title('model loss')
    plt.ylabel('loss')
    plt.xlabel('epoch')
    plt.legend(['train', 'test'], loc='upper left')
    plt.show()









    share|improve this question
























      0












      0








      0








      I wanted to split train and test data of a csv with tensorflow but I didn't find an order like np.loadtxt in tensor and tried to do splits with numpy and convert it to tensor, but I get an error like below:



       TypeError: object of type 'Tensor' has no len()


      and here is my code:



       import tensorflow as tf
      import numpy as np
      import matplotlib.pyplot as plt
      from sklearn.model_selection import train_test_split

      x = tf.convert_to_tensor( np.loadtxt('data.csv', delimiter=','))
      y = tf.convert_to_tensor(np.loadtxt('labels.csv', delimiter=','))

      x_train, x_test, y_train, y_test = train_test_split(x, y,
      test_size=0.25, random_state='')

      model = tf.keras.models.Sequential([
      tf.keras.layers.Flatten(input_shape= (426,30,1)),
      tf.keras.layers.Dense(126, activation=tf.nn.tanh),
      #tf.keras.layers.Dropout(0.2),
      tf.keras.layers.Dense(10, activation=tf.nn.tanh)
      ])

      model.compile(optimizer='sgd',
      loss='mean_squared_error',
      metrics=['accuracy'])

      history = model.fit(x_train, y_train, epochs=5 ) #validation_data
      = [x_test, y_test])
      model.evaluate(x_test, y_test)

      t_predicted = model.predict(x_test)
      out_predicted = np.argmax(t_predicted, axis=1)
      conf_matrix = tf.confusion_matrix(y_test, out_predicted)
      with tf.Session():
      print('Confusion Matrix: nn', tf.Tensor.eval(conf_matrix,
      feed_dict=None, session=None))

      # summarize history for accuracy
      plt.plot(history.history['acc'])
      plt.plot(history.history['val_acc'])
      plt.title('model accuracy')
      plt.ylabel('accuracy')
      plt.xlabel('epoch')
      plt.legend(['train', 'test'], loc='upper left')
      plt.show()

      # summarize history for loss
      plt.plot(history.history['loss'])
      plt.plot(history.history['val_loss'])
      plt.title('model loss')
      plt.ylabel('loss')
      plt.xlabel('epoch')
      plt.legend(['train', 'test'], loc='upper left')
      plt.show()









      share|improve this question














      I wanted to split train and test data of a csv with tensorflow but I didn't find an order like np.loadtxt in tensor and tried to do splits with numpy and convert it to tensor, but I get an error like below:



       TypeError: object of type 'Tensor' has no len()


      and here is my code:



       import tensorflow as tf
      import numpy as np
      import matplotlib.pyplot as plt
      from sklearn.model_selection import train_test_split

      x = tf.convert_to_tensor( np.loadtxt('data.csv', delimiter=','))
      y = tf.convert_to_tensor(np.loadtxt('labels.csv', delimiter=','))

      x_train, x_test, y_train, y_test = train_test_split(x, y,
      test_size=0.25, random_state='')

      model = tf.keras.models.Sequential([
      tf.keras.layers.Flatten(input_shape= (426,30,1)),
      tf.keras.layers.Dense(126, activation=tf.nn.tanh),
      #tf.keras.layers.Dropout(0.2),
      tf.keras.layers.Dense(10, activation=tf.nn.tanh)
      ])

      model.compile(optimizer='sgd',
      loss='mean_squared_error',
      metrics=['accuracy'])

      history = model.fit(x_train, y_train, epochs=5 ) #validation_data
      = [x_test, y_test])
      model.evaluate(x_test, y_test)

      t_predicted = model.predict(x_test)
      out_predicted = np.argmax(t_predicted, axis=1)
      conf_matrix = tf.confusion_matrix(y_test, out_predicted)
      with tf.Session():
      print('Confusion Matrix: nn', tf.Tensor.eval(conf_matrix,
      feed_dict=None, session=None))

      # summarize history for accuracy
      plt.plot(history.history['acc'])
      plt.plot(history.history['val_acc'])
      plt.title('model accuracy')
      plt.ylabel('accuracy')
      plt.xlabel('epoch')
      plt.legend(['train', 'test'], loc='upper left')
      plt.show()

      # summarize history for loss
      plt.plot(history.history['loss'])
      plt.plot(history.history['val_loss'])
      plt.title('model loss')
      plt.ylabel('loss')
      plt.xlabel('epoch')
      plt.legend(['train', 'test'], loc='upper left')
      plt.show()






      python tensorflow






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 24 at 8:24









      rezvaneh zahedirezvaneh zahedi

      1




      1






















          2 Answers
          2






          active

          oldest

          votes


















          0














          Won't it be simpler to first load the csv file, do the split and then give Tf the result of the split?



          sklearn.model_selection.train_test_split() is not meant to work with the Tensor objects you're getting from tf.convert_to_tensor().



          Reversing the order made your code work in small test script



          x = np.loadtxt('data.csv', delimiter=',')
          y = np.loadtxt('labels.csv', delimiter=',')

          x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25)

          x_train = tf.convert_to_tensor(x_train)
          x_test = tf.convert_to_tensor(x_test)
          y_train = tf.convert_to_tensor(y_train)
          y_test = tf.convert_to_tensor(y_test)





          share|improve this answer






























            0














            The best practice it not to load full data into tensor. If your code is executed on GPU and if you data is huge the tensor might occupy a significant amount of GPU memory result in "Out of Memory" errors. The normally used way is



            • Load the data into a RAM variable (usually numpy),

            • Split the data into train and validation batches (using something like train_test_split)

            • Iterate over splits with batch size and creating a tensor of batch size. Use it to train (validation split batch for validation)

            When dealing with large/huge images, we cannot load all the images into memory. Here the normally followed approached is to load a batch of images into a tensor and use it for training/validataion. All the deeplearning frameworks provide mechanisms to load multiple batches in multithreads so that the next train step is not waiting for the next batch to be loaded.



            If you still want to load the full data into a tensor and split it into train and test tensors then can use the tensorflow method tf.split. https://www.tensorflow.org/api_docs/python/tf/split






            share|improve this answer























              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%2f55321905%2fwant-to-split-train-and-test-data-gotten-from-a-csv-with-tensorflow%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              Won't it be simpler to first load the csv file, do the split and then give Tf the result of the split?



              sklearn.model_selection.train_test_split() is not meant to work with the Tensor objects you're getting from tf.convert_to_tensor().



              Reversing the order made your code work in small test script



              x = np.loadtxt('data.csv', delimiter=',')
              y = np.loadtxt('labels.csv', delimiter=',')

              x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25)

              x_train = tf.convert_to_tensor(x_train)
              x_test = tf.convert_to_tensor(x_test)
              y_train = tf.convert_to_tensor(y_train)
              y_test = tf.convert_to_tensor(y_test)





              share|improve this answer



























                0














                Won't it be simpler to first load the csv file, do the split and then give Tf the result of the split?



                sklearn.model_selection.train_test_split() is not meant to work with the Tensor objects you're getting from tf.convert_to_tensor().



                Reversing the order made your code work in small test script



                x = np.loadtxt('data.csv', delimiter=',')
                y = np.loadtxt('labels.csv', delimiter=',')

                x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25)

                x_train = tf.convert_to_tensor(x_train)
                x_test = tf.convert_to_tensor(x_test)
                y_train = tf.convert_to_tensor(y_train)
                y_test = tf.convert_to_tensor(y_test)





                share|improve this answer

























                  0












                  0








                  0







                  Won't it be simpler to first load the csv file, do the split and then give Tf the result of the split?



                  sklearn.model_selection.train_test_split() is not meant to work with the Tensor objects you're getting from tf.convert_to_tensor().



                  Reversing the order made your code work in small test script



                  x = np.loadtxt('data.csv', delimiter=',')
                  y = np.loadtxt('labels.csv', delimiter=',')

                  x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25)

                  x_train = tf.convert_to_tensor(x_train)
                  x_test = tf.convert_to_tensor(x_test)
                  y_train = tf.convert_to_tensor(y_train)
                  y_test = tf.convert_to_tensor(y_test)





                  share|improve this answer













                  Won't it be simpler to first load the csv file, do the split and then give Tf the result of the split?



                  sklearn.model_selection.train_test_split() is not meant to work with the Tensor objects you're getting from tf.convert_to_tensor().



                  Reversing the order made your code work in small test script



                  x = np.loadtxt('data.csv', delimiter=',')
                  y = np.loadtxt('labels.csv', delimiter=',')

                  x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25)

                  x_train = tf.convert_to_tensor(x_train)
                  x_test = tf.convert_to_tensor(x_test)
                  y_train = tf.convert_to_tensor(y_train)
                  y_test = tf.convert_to_tensor(y_test)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 24 at 8:44









                  Ido_fIdo_f

                  3991523




                  3991523























                      0














                      The best practice it not to load full data into tensor. If your code is executed on GPU and if you data is huge the tensor might occupy a significant amount of GPU memory result in "Out of Memory" errors. The normally used way is



                      • Load the data into a RAM variable (usually numpy),

                      • Split the data into train and validation batches (using something like train_test_split)

                      • Iterate over splits with batch size and creating a tensor of batch size. Use it to train (validation split batch for validation)

                      When dealing with large/huge images, we cannot load all the images into memory. Here the normally followed approached is to load a batch of images into a tensor and use it for training/validataion. All the deeplearning frameworks provide mechanisms to load multiple batches in multithreads so that the next train step is not waiting for the next batch to be loaded.



                      If you still want to load the full data into a tensor and split it into train and test tensors then can use the tensorflow method tf.split. https://www.tensorflow.org/api_docs/python/tf/split






                      share|improve this answer



























                        0














                        The best practice it not to load full data into tensor. If your code is executed on GPU and if you data is huge the tensor might occupy a significant amount of GPU memory result in "Out of Memory" errors. The normally used way is



                        • Load the data into a RAM variable (usually numpy),

                        • Split the data into train and validation batches (using something like train_test_split)

                        • Iterate over splits with batch size and creating a tensor of batch size. Use it to train (validation split batch for validation)

                        When dealing with large/huge images, we cannot load all the images into memory. Here the normally followed approached is to load a batch of images into a tensor and use it for training/validataion. All the deeplearning frameworks provide mechanisms to load multiple batches in multithreads so that the next train step is not waiting for the next batch to be loaded.



                        If you still want to load the full data into a tensor and split it into train and test tensors then can use the tensorflow method tf.split. https://www.tensorflow.org/api_docs/python/tf/split






                        share|improve this answer

























                          0












                          0








                          0







                          The best practice it not to load full data into tensor. If your code is executed on GPU and if you data is huge the tensor might occupy a significant amount of GPU memory result in "Out of Memory" errors. The normally used way is



                          • Load the data into a RAM variable (usually numpy),

                          • Split the data into train and validation batches (using something like train_test_split)

                          • Iterate over splits with batch size and creating a tensor of batch size. Use it to train (validation split batch for validation)

                          When dealing with large/huge images, we cannot load all the images into memory. Here the normally followed approached is to load a batch of images into a tensor and use it for training/validataion. All the deeplearning frameworks provide mechanisms to load multiple batches in multithreads so that the next train step is not waiting for the next batch to be loaded.



                          If you still want to load the full data into a tensor and split it into train and test tensors then can use the tensorflow method tf.split. https://www.tensorflow.org/api_docs/python/tf/split






                          share|improve this answer













                          The best practice it not to load full data into tensor. If your code is executed on GPU and if you data is huge the tensor might occupy a significant amount of GPU memory result in "Out of Memory" errors. The normally used way is



                          • Load the data into a RAM variable (usually numpy),

                          • Split the data into train and validation batches (using something like train_test_split)

                          • Iterate over splits with batch size and creating a tensor of batch size. Use it to train (validation split batch for validation)

                          When dealing with large/huge images, we cannot load all the images into memory. Here the normally followed approached is to load a batch of images into a tensor and use it for training/validataion. All the deeplearning frameworks provide mechanisms to load multiple batches in multithreads so that the next train step is not waiting for the next batch to be loaded.



                          If you still want to load the full data into a tensor and split it into train and test tensors then can use the tensorflow method tf.split. https://www.tensorflow.org/api_docs/python/tf/split







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Mar 24 at 9:04









                          mujjigamujjiga

                          3,68011321




                          3,68011321



























                              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%2f55321905%2fwant-to-split-train-and-test-data-gotten-from-a-csv-with-tensorflow%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

                              SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                              용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

                              155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해