【问题标题】:Reshape neural network input based on condition根据条件重塑神经网络输入
【发布时间】:2019-11-03 19:43:39
【问题描述】:

数值数据,使用深度学习神经网络。我为此目的使用 Keras 库

    p   u   d    ms          action B   x    y-c pre    area       finger 
0   0   36  3   1334893336790   0   1   262 262 262     0.044444    0.0
1   0   36  3   1334893336790   2   1   262 271 0.32    0.044444    0.0
2   0   36  3   1334893336795   2   1   123 327 0.28    0.044444    0.0
3   0   36  3   1334893336800   1   1   123 327 0.28    0.044444    0.0
4   0   36  3   1334893336885   0   1   216 298 0.34    0.044444    0.0
5   0   36  3   1334893336907   2   1   216 298 0.38    0.044444    0.0
6   0   36  3   1334893336926   2   1   147 312 0.60    0.088889    0.0
7   0   36  3   1334893336949   2   1   115 328 0.63    0.044444    0.0
8   0   36  3   1334893336952   2   1   98  336 0.17    0.133333    0.0
9   0   36  3   1334893336971   1   1   98  336 0.17    0.133333    0.0
1   0   36  3   1334893337798   0   1   108 339 0.48    0.044444    0.0

下面的代码可以工作,但是,据我所知,神经网络输入是逐行作为输入的,这里我试图根据 action column 进行输入和输出,如下例所示它以 0 开始并以 1 结束,然后包括第 [0 到 3] 行 3 的神经网络的第一个输入,第二个输入是 [4 到 9] 9 包括在内,依此类推... action列元素代表手部动作,如果值为0表示手的手指按在屏幕上,如果为1则表示手从屏幕上抬起,所以我尝试分成n个笔画,尝试在基于手指压力和提升(行程)的神经网络上进行输入,基于这个想法,输入将从 900k 减少到 20k,但每次输入都是基于多行的

the first input will be as below:
        p   u   d    ms          action B   x    y-c pre    area       finger 
    0   0   36  3   1334893336790   0   1   262 262 262     0.044444    0.0
    1   0   36  3   1334893336790   2   1   262 271 0.32    0.044444    0.0
    2   0   36  3   1334893336795   2   1   123 327 0.28    0.044444    0.0
    3   0   36  3   1334893336800   1   1   123 327 0.28    0.044444    0.0

and the second input will be :
        p   u   d    ms          action B   x    y-c pre    area       finger 
    4   0   36  3   1334893336885   0   1   216 298 0.34    0.044444    0.0
    5   0   36  3   1334893336907   2   1   216 298 0.38    0.044444    0.0
    6   0   36  3   1334893336926   2   1   147 312 0.60    0.088889    0.0
    7   0   36  3   1334893336949   2   1   115 328 0.63    0.044444    0.0
    8   0   36  3   1334893336952   2   1   98  336 0.17    0.133333    0.0
    9   0   36  3   1334893336971   1   1   98  336 0.17    0.133333    0.0

这是我的代码,它在 NN 的正常周期中运行良好,但我试图根据我的想法对其进行更改..

#o = no_of_click
o=0
lenf=len(dataset)
for h in dataset.index[dataset.iloc[:, 4] == 0]:
    if dataset.iloc[h+1,4]==1 :
        dataset.iloc[h+1,4]=-1
        dataset.iloc[h , 4] = -1
        o=o+1
dataset=dataset.drop(dataset[dataset.iloc[:, 4] == -1].index)
lenf=(o*2)
X = dataset.iloc[:, 2:].values #here 3to 11 for x
y = dataset.iloc[:, 1].values #here user id 2 only y

binariz = LabelBinarizer()
s = binariz.fit_transform(X[:, 0])
X = np.delete(X, [0], axis=1)
X = np.hstack([s,X])
y = binariz.fit_transform(y)
# X Features scaling
sc_X = StandardScaler()
X = sc_X.fit_transform(X)

# Splitting Data
X_train, X_test,y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)

classifier = Sequential()
# Adding the input layer and the first hidden layer
classifier.add(Dense(units = 50, activation = 'relu', input_dim = X_train.shape[1]))
# Adding the second hidden layer

classifier.add(Dense(units = 50, activation = 'relu'))
# Adding the output layer
classifier.add(Dense(units = y.shape[1], activation = 'softmax'))
# Compiling the ANN
classifier.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
# Fitting the ANN to the Training set
classifier.fit(X_train, y_train, batch_size = 100, epochs = 10)

【问题讨论】:

  • 离题 - 我看到你删除了你的 TAP3 问题 - 如果你在未来的某个时候愿意,我仍然很乐意提供帮助。祝你好运!
  • 谢谢兄弟,我已经解决了这个问题,所以你是个好人
  • mashro3ak,很高兴听到它!您愿意取消删除您的问题并将您的解决方案作为答案发布吗?这样,它可以帮助可能有类似问题的其他人。 (另外,您可以从对您的问题或答案的投票中获得一些声誉。:))见stackoverflow.com/help/self-answer。当然取决于你! (也谢谢你的好话!)
  • 我已经发布了这个问题作为答案;)

标签: numpy tensorflow keras neural-network deep-learning


【解决方案1】:

我不确定我是否正确理解了您的问题;如果我做出不正确的假设,请提前道歉。

在我看来,您是在问是否可以重新调整输入向量的形状,以便在一种情况下它具有 shape=(4,) 而在另一种情况下具有 shape=(6,)。

我不相信你可以,因为当你在输入层之后添加一个密集层时,这个密集层有一个权重矩阵,其形状为 (input_dims, output_dims)。这是在构建图表时选择的。

即使你可以,我想你也不会想要。 NN 的输入向量是一组特征;在您的情况下,这似乎是一组不同的测量值。您不想在一种情况下向网络提供输入张量位置 0 中的 feature0 测量值和另一种情况下的 feature4 测量值。这使得网络更难理解如何处理这些值。

鉴于您有一小部分功能,您是否有任何理由不一直传递所有数据?

【讨论】:

  • action列中的元素代表手的运动,如果值为0表示手的手指按在屏幕上,如果为1则表示手从屏幕上抬起,所以我尝试分成n个笔画,尝试根据手指压力和提升(笔画)在神经网络上进行输入,基于这个想法,输入将从900k减少到20k,但每次输入将是基于多行的,如讨论,我会更新我的问题,请稍后查看
猜你喜欢
  • 2019-08-14
  • 2021-06-28
  • 2012-10-05
  • 1970-01-01
  • 2010-10-13
  • 2018-07-25
  • 2019-05-22
  • 2017-12-25
相关资源
最近更新 更多