Skip to content

Commit

Permalink
submit hw7
Browse files Browse the repository at this point in the history
  • Loading branch information
wenqilyu committed Apr 19, 2021
1 parent ac5ffd7 commit cd6520c
Show file tree
Hide file tree
Showing 2 changed files with 640 additions and 0 deletions.
112 changes: 112 additions & 0 deletions hw7/hw7-wenqi.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
---
title: "hw7-wenqi"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```


```{r}
library(keras)
library('rgl')
library('ElemStatLearn')
library('nnet')
library('dplyr')
## load binary classification example data
data("mixture.example")
dat <- mixture.example
```

```{r}
img_dat <- matrix(nrow=200,ncol=2)
img_dat[,1]<- dat$x[,1]
img_dat[,2]<- dat$x[,2]
```


```{r}
model <- keras_model_sequential()
model <- model %>%
layer_flatten(input_shape = c(2, 200)) %>%
layer_dense(units = 128, activation = 'relu') %>%
layer_dense(units = 10, activation = 'softmax')
model <- model %>% compile(
optimizer = 'adam',
loss = 'sparse_categorical_crossentropy',
metrics = c('accuracy')
)
```





```{r}
## create 3D plot of mixture data
plot_mixture_data <- function(dat=mixture.example, showtruth=FALSE) {
## create 3D graphic, rotate to view 2D x1/x2 projection
par3d(FOV=1,userMatrix=diag(4))
plot3d(dat$xnew[,1], dat$xnew[,2], dat$prob, type="n",
xlab="x1", ylab="x2", zlab="",
axes=FALSE, box=TRUE, aspect=1)
## plot points and bounding box
x1r <- range(dat$px1)
x2r <- range(dat$px2)
pts <- plot3d(dat$x[,1], dat$x[,2], 1,
type="p", radius=0.5, add=TRUE,
col=ifelse(dat$y, "orange", "blue"))
lns <- lines3d(x1r[c(1,2,2,1,1)], x2r[c(1,1,2,2,1)], 1)
if(showtruth) {
## draw Bayes (True) classification boundary
probm <- matrix(dat$prob, length(dat$px1), length(dat$px2))
cls <- contourLines(dat$px1, dat$px2, probm, levels=0.5)
pls <- lapply(cls, function(p)
lines3d(p$x, p$y, z=1, col='purple', lwd=3))
## plot marginal probability surface and decision plane
sfc <- surface3d(dat$px1, dat$px2, dat$prob, alpha=1.0,
color="gray", specular="gray")
qds <- quads3d(x1r[c(1,2,2,1)], x2r[c(1,1,2,2)], 0.5, alpha=0.4,
color="gray", lit=FALSE)
}
}
## compute and plot predictions
plot_nnet_predictions <- function(fit, dat=mixture.example) {
## create figure
plot_mixture_data()
## compute predictions from nnet
preds <- predict(fit, dat$xnew, type="class")
probs <- predict(fit, dat$xnew, type="raw")[,1]
probm <- matrix(probs, length(dat$px1), length(dat$px2))
cls <- contourLines(dat$px1, dat$px2, probm, levels=0.5)
## plot classification boundary
pls <- lapply(cls, function(p)
lines3d(p$x, p$y, z=1, col='purple', lwd=2))
## plot probability surface and decision plane
sfc <- surface3d(dat$px1, dat$px2, probs, alpha=1.0,
color="gray", specular="gray")
qds <- quads3d(x1r[c(1,2,2,1)], x2r[c(1,1,2,2)], 0.5, alpha=0.4,
color="gray", lit=FALSE)
}
## plot data and 'true' probability surface
plot_mixture_data(showtruth=TRUE)
## fit single hidden layer, fully connected NN
## 10 hidden nodes
fit <- nnet(x=dat$x, y=dat$y, size=10, entropy=TRUE, decay=0)
```

Loading

0 comments on commit cd6520c

Please sign in to comment.