分割数据的神经网络

Neural network on split data

我已经将图像样本分成 70% 的训练和 30% 的测试

 #using this for question one with neural network
 originaldata_train, originaldata_test, targetoriginaldata_train,     targetoriginaldata_test = train_test_split(originalrepo, 
                                                                      target, test_size=0.3, 
                                                                      random_state=42, stratify=target)

  bindata_train, bindata_test, targetbindata_train, targetbindata_test = train_test_split(binarisedrepo, 
                                                                      target, test_size=0.3, 
                                                                      random_state=42, stratify=target)

我将二进制文件和原始版本分开了。我想在其中一个上应用神经网络。

我用的是keras

 model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(16,(3,3),activation = "relu" , input_shape = (180,180,3)) ,
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(32,(3,3),activation = "relu") ,  
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(64,(3,3),activation = "relu") ,  
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(128,(3,3),activation = "relu"),  
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Flatten(), 
tf.keras.layers.Dense(550,activation="relu"),      #Adding the Hidden layer
tf.keras.layers.Dropout(0.1,seed = 2019),
tf.keras.layers.Dense(400,activation ="relu"),
tf.keras.layers.Dropout(0.3,seed = 2019),
tf.keras.layers.Dense(300,activation="relu"),
tf.keras.layers.Dropout(0.4,seed = 2019),
tf.keras.layers.Dense(200,activation ="relu"),
tf.keras.layers.Dropout(0.2,seed = 2019),
tf.keras.layers.Dense(5,activation = "softmax")   #Adding the Output Layer
])


from tensorflow.keras.optimizers import RMSprop,SGD,Adam
adam=Adam(lr=0.001)
model.compile(optimizer='adam',loss =     tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy'])


history = model.fit(x_train,y_train,epochs = 500 , validation_data = (x_val, y_val))

但是我遇到了一些错误

  <ipython-input-76-33734b1da1bc> in <module>()
  ----> 1 history = model.fit(x_train,y_train,epochs = 500 ,     validation_data = (x_val, y_val))

   1 frames
  /usr/local/lib/python3.7/dist-         packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
  1145           except Exception as e:  # pylint:disable=broad-   except
  1146             if hasattr(e, "ag_error_metadata"):
-> 1147               raise e.ag_error_metadata.to_exception(e)
   1148             else:
   1149               raise

ValueError:在用户代码中:

File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function  *
    return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function  **
    outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step  **
    outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 859, in train_step
    y_pred = self(x, training=True)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 264, in assert_input_compatibility
    raise ValueError(f'Input {input_index} of layer "{layer_name}" is '

ValueError: Input 0 of layer "sequential_5" is incompatible with the layer: expected shape=(None, 180, 180, 3), found shape=(None, 10000)

任何人有更好的方法来解决这个问题或我需要做什么

错误消息告诉您输入数据的格式错误。看来不是算法错了,而是你给它的数据错了。

它需要一个形状数组 (180,180,3),也许是彩色图像?而你给它一些带有 10000 元素的平面数组。

Double-check 您正在使用的输入;应该是错的。

I suspect you are giving the array of imageS (i.e, the list of image arrays, instead of the proper/individual array).

算法很好,但问题是您告诉算法使用形状 (180, 180, 3) 但您输入的形状是 10000。

tf.keras.layers.Conv2D(16,(3,3),activation = "relu" , input_shape = (180,180,3)) ,

计算一下

180 * 180 * 3 = 97,200. which is not equals to 10,000.

现在试试这个

步骤

  • 首先使用
  • 将您的数据转换为 numpy 数组

import numpy as np. np.array(originaldata_train)

  • 试试这个来了解你可以使用的形状

print(originaldata_train.shape).. this will give you a clue on size you can use . e.g (230,390,1)

  • 这是您可以使用的尺寸。您可能想确定并仍然使用
  • 再次重置它

请记住,您正在使用打印的值进行整形,而不是 1852、32、1

originaldata_train = originaldata_train.reshape(1852, 32, 1)

  • 现在将其转换为浮点数

train_images = originaldata_train.astype('float32')

  • 在这里转换

train_images /= 255

  • 您现在可以将其提供给您的算法

    model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(16,(3,3),activation = "relu" , input_shape = 
      #this will be the shape you set above not 1852, 32, 1
    ( 1852, 32, 1)) ,
     tf.keras.layers.MaxPooling2D(2,2),
    

再试一次。

如果您遇到困难或需要参考,请试试这个 link Sample classification of Images with Neural Network