使用神经网络预测伪随机数的好方法是什么?

今天我尝试使用神经网络来预测基于前20个随机数序列生成的下一个随机数。然而,当我实际使用模型进行预测时,它只是返回了最常见的数字。我对keras和tensorflow非常陌生,所以我不知道如何解决这个问题,或者这是否是可能的。以下是代码。

from random import randintimport numpy as npimport tensorflow as tfdef generate_sequence(length=1000000):    rList = []    for i in range(length):        rInt = randint(0, 99)        if rInt >= 0 and rInt <= 47:            rList.append(0)        elif rInt > 47 and rInt <= 71:            rList.append(1)        elif rInt > 71 and rInt <= 87:            rList.append(2)        elif rInt > 87 and rInt <= 95:            rList.append(3)        else:            rList.append(4)    return rListdata = generate_sequence()train_data = data[:800000]test_data = data[800000:]x_train = []y_train = []x_test = []y_test = []for i in range(0, 799980, 20):    x_train.append(train_data[i:i+20])    y_train.append(train_data[i+20])for i in range(0, 199980, 20):    x_test.append(test_data[i:i+20])    y_test.append(test_data[i+20])x_train = tf.keras.utils.normalize(x_train, axis=1)x_test = tf.keras.utils.normalize(x_test, axis=1)x_train = np.asarray(x_train)y_train = np.asarray(y_train)x_test = np.asarray(x_test)y_test = np.asarray(y_test)model = tf.keras.models.Sequential()model.add(tf.keras.layers.Flatten())model.add(tf.keras.layers.Dense(625, activation=tf.nn.relu))model.add(tf.keras.layers.Dense(125, activation=tf.nn.relu))model.add(tf.keras.layers.Dense(25, activation=tf.nn.relu))model.add(tf.keras.layers.Dense(5, activation=tf.nn.softmax))model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])model.fit(x_train, y_train, epochs=5)val_loss, val_acc = model.evaluate(x_test, y_test)print(val_loss, val_acc)prediction = model.predict([x_test])for pred in prediction:    print(np.argmax(pred))

回答:

我认为你无法根据过去的输出预测Python的random模块生成的数字。random模块使用的是random,它采用梅森旋转算法,在生成2^19937-1个数字之前不会重复。神经网络无法基于之前的数字预测接下来的数字。

你的神经网络返回最常见数字的原因是,由于任何给定的20个前序数字与后续数字之间没有相关性,神经网络所能做的最好选择就是猜测最常见的数字,因为这在训练中会得到最高的得分。

如果你的目标是破解Python的random模块,或许这个回答建议计算梅森旋转算法的状态可能会有帮助。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注