python - Keras 神经网络中的模型精度非常低和计算错误
问题描述
我在 Keras/tf 中编写了一个神经网络模型,它计算随机二项式数据集的偏差和方差并将它们平均,方差应该是偏差的两倍(对数似然比),但是,我的模型表现不佳巨额。它neuralnet
使用 RProp 运行良好,但它不在 Keras 中,使用 rmsprop(或 SGD)。此外,我的模型没有超过 50% 的准确率。
可重现的代码:
library(reticulate)
library(keras)
library(tensorflow)
# library(caret)
use_python("C:/mini/envs/aiml3", required=T)
Sys.setenv(RETICULATE_MINICONDA_PATH = "C:/mini/envs/aiml3")
###DATA CREATION
input <- 2 #number of inputs into NN
n <- 2000 #number of observations
ndata <- 500 #number of simulations
nvar <- input + 1 #number of inputs (x) plus one (y) e.g. 5+1=6 for formula y~sum(x_n)
row.names <- c(1:n)
column.names <- c(1:nvar)
matrix.names <- c(1:ndata)
datas <- array(0,dim=c(n,nvar,ndata), dimnames = list(row.names, column.names, matrix.names))
for(i in 1:ndata){
datas[,,i] = rbinom(n*nvar,1,0.5)
dim(datas[,,i]) = c(n,nvar)
}
result <- matrix(nrow = ndata, ncol = 1)
colnames(result) <- c("D")
# source_python("C:\\mini\\envs\\aiml3\\Lib\\site-packages\\tensorflow_core\\python\\keras\\optimizer_v2\\rprop.py")
# myopt = RProp(name="rprop") #attempt to use rprop optimizer
model <- keras_model_sequential() %>%
layer_dense(units = 2, activation = "sigmoid", input_shape = c(2)) %>% #logistic model, input: #1 hidden layer, 2 hidden units
layer_dense(units = 1, activation = "sigmoid") #output
model %>% compile(
optimizer = rmsprop, #is this the best choice?
loss = "binary_crossentropy", #^
metrics = c("accuracy")
)
for(i in 1:20){ #change to 1:ndata, 20 is just for testing
y <- datas[,nvar,i]
n1 <- sum(y) #sum y as the third nth column of the ith matrix
n0 <- n-n1
seed <- 1020 + i
set.seed(seed)
currentmatrix <- datas[,,i]
trainee <- currentmatrix[,-3] #change
y <- as.matrix(y)
history <- model %>% fit(
trainee,
y,
epochs = 1,
batch_size = 2000,
)
predictions <- model %>% predict(trainee) #currentmatrix[,1:nvar-1]
argument <- y*log(predictions[,1])+(1-y)*log(1-predictions[,1])
result[i] <- -2*((sum(argument))-(n1*log(n1)+n0*log(n0)-n*log(n))) #calculate and store deviance
}
df <- mean(result[,1], na.rm = T); print(var(result[,1], na.rm = T)); print(df); print((var(result[,1], na.rm = T))/df) #var/def should be ~2
####output####
print(n)
print(df)
print(var(result[,1], na.rm = T))
print(summary(result[,1]))
TL;DR:神经网络的准确性很差,并且计算的统计数据错误。只有在 Keras,不知道为什么。怀疑优化器/激活/简单的东西有问题。
解决方案
推荐阅读
- search - 西班牙语字符上的 Solr IOException(电影示例)
- html - 如何修复路径 url 中区分大小写的问题?
- qt - ubuntu 上 cmake 和 Qt5 的问题
- javascript - 在 javascript 中使用正则表达式进行搜索和替换
- reactjs - 更新 React 中删除时显示的组件列表
- python - 无法通过python连接mysql
- ios - iOS图表:条形图间距和位置
- c# - 字符串(不仅仅是字母)到 ConsoleKey c#
- docker - Docker COPY 命令未挂载目录
- google-cloud-firestore - Cloud Firestore,独立的个人数据库