R语言神经网络keras,tensorflow,mxnet
来源:互联网 发布:番茄时钟软件 pc 编辑:程序博客网 时间:2024/04/29 09:42
#loading the keras inbuilt mnist dataset
data<-dataset_mnist()
#separating train and test file
train_x<-data$train$x
train_y<-data$train$y
test_x<-data$test$x
test_y<-data$test$y
rm(data)
# converting a 2D array into a 1D array for feeding into the MLP and normalising the matrix
train_x <- array(train_x, dim = c(dim(train_x)[1], prod(dim(train_x)[-1]))) / 255
test_x <- array(test_x, dim = c(dim(test_x)[1], prod(dim(test_x)[-1]))) / 255
#converting the target variable to once hot encoded vectors using keras inbuilt function
train_y<-to_categorical(train_y,10)
test_y<-to_categorical(test_y,10)
#defining a keras sequential model
model <- keras_model_sequential()
#defining the model with 1 input layer[784 neurons], 1 hidden layer[784 neurons] with dropout rate 0.4 and 1 output layer[10 neurons]
#i.e number of digits from 0 to 9
model %>%
layer_dense(units = 784, input_shape = 784) %>%
layer_dropout(rate=0.4)%>%
layer_activation(activation = 'relu') %>%
layer_dense(units = 10) %>%
layer_activation(activation = 'softmax')
#compiling the defined model with metric = accuracy and optimiser as adam.
model %>% compile(
loss = 'categorical_crossentropy',
optimizer = 'adam',
metrics = c('accuracy')
)
#fitting the model on the training dataset
model %>% fit(train_x, train_y, epochs = 100, batch_size = 128)
#Evaluating model on the cross validation dataset
loss_and_metrics <- model %>% evaluate(test_x, test_y, batch_size = 128)
###################################################################
library(tensorflow)
sess = tf$Session()
hello <- tf$constant('Hello, TensorFlow!')
sess$run(hello)
library(tensorflow)
# Create 100 phony x, y data points, y = x * 0.1 + 0.3
x_data <- runif(100, min=0, max=1)
y_data <- x_data * 0.1 + 0.3
# Try to find values for W and b that compute y_data = W * x_data + b
# (We know that W should be 0.1 and b 0.3, but TensorFlow will
# figure that out for us.)
W <- tf$Variable(tf$random_uniform(shape(1L), -1.0, 1.0))
b <- tf$Variable(tf$zeros(shape(1L)))
y <- W * x_data + b
# Minimize the mean squared errors.
loss <- tf$reduce_mean((y - y_data) ^ 2)
optimizer <- tf$train$GradientDescentOptimizer(0.5)
train <- optimizer$minimize(loss)
# Launch the graph and initialize the variables.
sess = tf$Session()
sess$run(tf$initialize_all_variables())
# Fit the line (Learns best fit is W: 0.1, b: 0.3)
for (step in 1:201) {
sess$run(train)
if (step %% 20 == 0)
cat(step, "-", sess$run(W), sess$run(b), "\n")
}
################################################################
library(mlbench)
library(mxnet)
data(Sonar, package="mlbench")
str(Sonar)
Sonar[,61] = as.numeric(Sonar[,61])-1
train.ind = c(1:50, 100:150)
train.x = data.matrix(Sonar[train.ind, 1:60])
train.y = Sonar[train.ind, 61]
test.x = data.matrix(Sonar[-train.ind, 1:60])
test.y = Sonar[-train.ind, 61]
mx.set.seed(0)
# 模型,num.round表示迭代数,array.batch.size表示批规模
model <- mx.mlp(train.x, train.y, hidden_node=10, out_node=2,out_activation="softmax",
num.round=20, array.batch.size=15, learning.rate=0.07, momentum=0.9, eval.metric=mx.metric.accuracy)
preds = predict(model, test.x)
pred.label = max.col(t(preds))-1
table(pred.label, test.y)
(24+33) / (24+14+36+33)
- R语言神经网络keras,tensorflow,mxnet
- R语言安装mxnet问题
- Mask R-CNN Keras Tensorflow实现
- MXNet | 在R语言中使用
- R语言深度学习mxnet做分类
- R语言实现神经网络
- R语言:神经网络
- R语言神经网络
- r语言神经网络
- MxNet+R︱用R语言实现深度学习
- 利用tensorflow keras搭建一个简单的卷积神经网络
- 深度工具合集安装(Nvidia+CUDA+cuDNN+Tensorflow+OpenBLAS+Caffe+Theano+Keras+Torch+Mxnet+X2Go)
- 深度工具合集安装(Nvidia+CUDA+cuDNN+Tensorflow+OpenBLAS+Caffe+Theano+Keras+Torch+Mxnet+X2Go)
- TensorFlow和Caffe、MXNet、Keras等其他深度学习框架的对比
- TensorFlow和Caffe、MXNet、Keras等其他深度学习框架的对比
- TensorFlow和Caffe、MXNet、Keras等其他深度学习框架的对比
- TensorFlow和Caffe、MXNet、Keras等深度学习框架的对比
- TensorFlow和Caffe、MXNet、Keras等其他深度学习框架的对比
- jquery新手入门教学(三)
- android studio 创建java项目
- hive
- 11.7
- ubuntu 14.04 搜狗拼音输入法安装
- R语言神经网络keras,tensorflow,mxnet
- 栈的应用--逆波兰表达式
- 有功功率,无功功率,视在功率定义
- 微信小程序选项卡原理
- Pyro4分布式计算入门例子
- Tableview偏移的误差,偏移20,偏移64
- clockid_t与clock_gettime 函数
- WebService(6)——webService面试题
- Java学习笔记(8) -Java GUI设计与事件处理