Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我如何改进这个Keras模型,它可以预测给定的数字是奇数还是偶数_Python_Tensorflow_Keras - Fatal编程技术网

Python 我如何改进这个Keras模型,它可以预测给定的数字是奇数还是偶数

Python 我如何改进这个Keras模型,它可以预测给定的数字是奇数还是偶数,python,tensorflow,keras,Python,Tensorflow,Keras,我试图创建一个tensorflow keras深度学习模型来预测一个数字是奇数还是偶数。然而,我不知道如何使它真正起作用。目前的准确率在40-60%之间,这让我认为这只是猜测。如何改进此模型?我也不知道使用什么样的损失函数。这是我的第一个模型,所以我是一个非常noob。我也尝试过改变图层的大小,但效果似乎也不大。我还尝试过改变样本数据的大小(随机数,带有奇数或偶数的标签列表)。我只用了50到10000个。非常感谢您的帮助 代码: 您的问题标题和描述有所不同。你是想预测奇数对偶数还是正对负?奇数对

我试图创建一个tensorflow keras深度学习模型来预测一个数字是奇数还是偶数。然而,我不知道如何使它真正起作用。目前的准确率在40-60%之间,这让我认为这只是猜测。如何改进此模型?我也不知道使用什么样的损失函数。这是我的第一个模型,所以我是一个非常noob。我也尝试过改变图层的大小,但效果似乎也不大。我还尝试过改变样本数据的大小(随机数,带有奇数或偶数的标签列表)。我只用了50到10000个。非常感谢您的帮助

代码:


您的问题标题和描述有所不同。你是想预测奇数对偶数还是正对负?奇数对偶数。这是我的错。“我怎样才能改进这个模型?”好吧,你可以将数字建模为二进制标志,这取决于它们是奇数还是偶数,这使得你的模型变得多余。我知道这不是你想要的答案,但这可能是你没有得到有用答案的原因。问我如何将机器学习应用到不应该使用机器学习的问题对我来说有点奇怪。
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt

#The next few lines just import and preprocess the data

dataFile = open('oddEvenData.txt','r')
dataLabels = open('oddEvenLabels.txt','r')

data = dataFile.read()
labels = dataLabels.read()

train_data = data.split(',')
train_labels = labels.split(',')

train_data.remove('')
train_labels.remove('')

train_data = [int(x) for x in train_data]
train_labels = [int(x) for x in train_labels]

dataFile.close()
dataLabels.close()

#Creates and pre process's test data set

test_data = [[123],[56],[273],[834],[778]]
test_labels = [[0],[1],[0],[1],[1]]

test_data = np.array(test_data)
test_labels = np.array(test_labels)

test_data = test_data/10000

#Builds framework

model = keras.Sequential([
                          keras.layers.Dense(32, activation = 'relu',input_shape = (1,)),
                          keras.layers.Dense(128,activation = 'relu'),
                          keras.layers.Dense(64,activation = 'relu'),
                          keras.layers.Dense(2,activation = 'softmax')
])


model.compile(optimizer = 'adam',loss = 'sparse_categorical_crossentropy',metrics = ['accuracy'])


model.fit(train_data,train_labels,epochs = 10)

test_loss,test_acc = model.evaluate(test_data,test_labels)

print('Tested Acc:'+str(test_acc))

testNum = [int(input('Input test num: '))]

prediction = model.predict(np.array(testNum))
print(prediction)