英文:
TensorFlow classifiying integers for divisibility by 3 does not work
问题
我想学习TensorFlow。我找到了一段代码,用于按2的整除性对整数进行分类这里。它工作得很好,准确率为100%。我只需要在开头添加一个用于导入numpy的命令。
现在我想将其更改为按3的整除性进行分类,而不是2。我只更改了一行代码。我将
Y.append( to_categorical(v%2, 2) )
更改为
Y.append( to_categorical(0 if v%3 == 0 else 1, 2) )
但现在它不再工作了。它总是预测1,准确率为0.67。我为什么会这样,因为我没有更改代码的风格?我只更改了分类函数。我尝试使用不同的损失函数,添加一个隐藏层以及不同的激活函数,但都没有帮助。我想知道为什么在应用这个小改变后,代码不再工作。这是我的代码:
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.utils import to_categorical
# 将数字转换为固定宽度的二进制表示的辅助函数
def conv(x):
a = format(x, '032b')
l = list(str(a))
l = np.array(list(map(int, l)))
return l
# 输入数据
data = [conv(i) for i in range(100000)]
X = np.array(data)
Y = list() # 结果的空列表
for v in range(100000):
Y.append(to_categorical(0 if v % 3 == 0 else 1, 2))
Y = np.array(Y) # 我们需要np.array
# Sequential是一个全连接网络
model = Sequential()
# 第一层(隐藏层)有32个输入和1个神经元
model.add(Dense(1, input_dim=32, activation='relu'))
# 输出层有2个神经元
model.add(Dense(2, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# epochs是重复训练相同数据集的次数
# batch_size是一次并行处理的元素数量
model.fit(X, Y, epochs=5, batch_size=100, verbose=1)
weights, biases = model.layers[0].get_weights()
print("weights", weights.size, weights, "biases", biases)
model.summary()
我还阅读了神经网络总是预测相同类别,但没有什么帮助。
英文:
I want to learn TensorFlow. I found a code which classifies integers for divisibility by 2 here. It works very well and accuracy is 100%. I only had to add an import command for numpy at the very beginning.
Now I wanted to change it to classify for divisibility by 3 instead of 2. I changed one single line. I changed
Y.append( to_categorical(v%2, 2) )
to
Y.append( to_categorical(0 if v%3 == 0 else 1, 2) )
But now it no longer works. It always predicts 1 and the accuracy is 0.67. How can that be as I didn't change the style of the code? I only changed the classification function. I tried to use different loss functions, add an hidden layer and also different activation functions. Nothing helped. I want to know why the code no longer works after applying this little change. Here is my code:
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.utils import to_categorical
# Helper function to convert a number
# to its fixed width binary representation
def conv(x):
a = format(x, '032b')
l = list(str(a))
l = np.array(list(map(int, l)))
return l
# input data
data = [conv(i) for i in range(100000)]
X = np.array(data)
Y= list() # empty list of results
for v in range(100000):
Y.append( to_categorical(0 if v%3 == 0 else 1, 2) )
Y = np.array(Y) # we need np.array
# Sequential is a fully connected network
model = Sequential()
# 32 inputs and 1 neuron in the first layer (hidden layer)
model.add(Dense(1, input_dim=32, activation='relu'))
# 2 output layer
model.add(Dense(2, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
# epochs is the number of times to retrain over the same data set
# batch_size is how may elements to process in parallel at one go
model.fit(X, Y, epochs=5, batch_size=100, verbose=1)
weights, biases = model.layers[0].get_weights()
print("weights",weights.size, weights, "biases", biases)
model.summary()
I have also read Neural network always predicts the same class but nothing helped.
答案1
得分: 1
在这段代码中,你使用了一个数字的二进制表示,并检查该数字是否能被2整除,使用这种表示方法相当简单 - 你检查最不重要的字节。对于任何2的幂次方,它都是类似的。
然而,对于3的整除性就不那么简单,因为这里的规则更加复杂,你可能需要尝试一些更复杂的模型。
例如,增加更多的隐藏层可以改善我们的结果。我使用了这个模型:
model = Sequential()
model.add(Dense(32, input_dim=32, activation='relu'))
model.add(Dense(32, activation='relu'))
model add(Dense(2, activation='sigmoid'))
并获得了以下结果:
...
Epoch 20/20
1000/1000 [==============================] - 1s 1ms/step - loss: 0.0594 - accuracy: 0.9700
你可以尝试不同的架构来获得更好的分数。
英文:
In this code you use binary representation of a number and checking if such number is divisible by 2 using this representation is pretty simple - you check least significant byte. For any power of two it is similar.
However, divisibility by 3 is not as simple because rule here is more complicated and you may try some more complex model.
For example adding more hidden layers can improve our results. I used this model:
model = Sequential()
model.add(Dense(32, input_dim=32, activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(2, activation='sigmoid'))
and got results:
...
Epoch 20/20
1000/1000 [==============================] - 1s 1ms/step - loss: 0.0594 - accuracy: 0.9700
You can try with different architectures to get better score.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论