-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirisdeeplearininginR.R
60 lines (32 loc) · 1.03 KB
/
irisdeeplearininginR.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
library(keras)
data1<-(iris)
data1<-as.matrix(data1)
split<-sample(1:2,nrow(data1),prob = c(0.8,0.2),replace = T)
train<-data1[split==1,]
test<-data1[split==2,]
trainx<-train[,1:4]
testx<-test[,1:4]
train[,5]<-as.numeric(as.factor(train[,5]))
trainy<-as.numeric(train[,5])
trainy<-trainy-1
testy<-as.numeric(as.factor(test[,5]))
testy<-testy-1
train_label<-to_categorical(trainy,num_classes = 3)
test_label<- to_categorical(testy,num_classes = 3)
model<-keras_model_sequential()
model %>%
layer_dense(units = 10,activation = 'relu',input_shape = c(4)) %>%
layer_dense(units = 10, activation = 'relu')%>%
layer_dense(units = 10, activation = 'relu')%>%
layer_dense(units = 3, activation = 'softmax')
summary(model)
model %>% compile(
loss='categorical_crossentropy',
optimizer=optimizer_rmsprop(),
metrics=c('accuracy')
)
history <- model %>% fit(trainx,
train_label,epochs=200,batch_size=5,validation_split=0.2)
plot(history)
y_data_pred=predict_classes(model,testx)
table(y_data_pred,testy)