Wavenet은 2016년에 DeepMind에서 만들어낸 모형으로, 음성의 파형을 모형으로 사용하여 실제 사람과 같은 음성을 생성하는 알고리즘이다.
WaveNet - Wikipedia
From Wikipedia, the free encyclopedia Jump to navigation Jump to search Deep neural network for generating raw audio WaveNet is a deep neural network for generating raw audio. It was created by researchers at London-based artificial intelligence firm DeepM
en.wikipedia.org
시계열에 활용될 수 있다고 하여 사용해 보았다.
아래는 dilation_rate만을 조절한 코드이다.
library('reticulate')
library('tensorflow')
library('keras')
library(abind)
data('AirPassengers')
ap_ts <- window(AirPassengers, start=1949, end=1955.99)
ap_ts_test <- window(AirPassengers, start=1956)
data_generate=function(data){
tr=as.vector(data)
tr_xs=list();tr_ys=list()
for(i in 1:(length(tr)-36)){
tr_xs[[i]]=tr[i:(i+23)]
tr_ys[[i]]=tr[(i+13):(i+36)]
}
tr_x=t(data.frame(tr_xs))
tr_y=t(data.frame(tr_ys))
rownames(tr_x)=NULL
rownames(tr_y)=NULL
tr_x=array(tr_x,c(nrow(tr_x),ncol(tr_x),1))
tr_y=array(tr_y,c(nrow(tr_y),ncol(tr_y),1))
return(list(tr_x,tr_y))
}
tr_=data_generate(ap_ts)
tr_x=tr_[[1]];tr_y=tr_[[2]]
dim(tr_x);dim(tr_y)
te_=data_generate(ap_ts_test)
te_x=te_[[1]];te_y=te_[[2]]
model=keras_model_sequential()
model$add(layer_input(shape=c(24,1)))
for(rate in rep(c(1,2,4,8),2)){
model$add(layer_conv_1d(filters=20,kernel_size = 2,padding='causal',
activation='relu',dilation_rate = rate))
}
model$add(layer_conv_1d(filters=1, kernel_size=1))
model%>%compile(
loss='mae',
optimizer='adam',
metrics=c('mae')
)
#kerasR::plot_model(model,to_file='/home/ducj/jupyter/R/tensorflow/model.png',show_shapes = T)
dim(tr_x);dim(tr_y[,,1])
model%>%fit(x = tr_x,y=tr_y,batch_size = 3,epochs = 600)
n=5
dim(te_y)
ylim_=c(min(c((model%>%predict(te_x))[n,,1]),te_y[n,,1]),max(c((model%>%predict(te_x))[n,,1]),te_y[n,,1]))
plot((model%>%predict(te_x))[n,,1],type='l',ylim=ylim_);lines(te_y[n,,1],col=2)
친절히 누군가가 패키지를 만들어주셔서 활용해보았다.
library('reticulate')
library('tensorflow')
library('keras')
library(abind)
data('AirPassengers')
ap_ts <- window(AirPassengers, start=1949, end=1955.99)
ap_ts_test <- window(AirPassengers, start=1956)
data_generate=function(data){
tr=as.vector(data)
tr_xs=list();tr_ys=list()
for(i in 1:(length(tr)-36)){
tr_xs[[i]]=tr[i:(i+23)]
tr_ys[[i]]=tr[(i+13):(i+36)]
}
tr_x=t(data.frame(tr_xs))
tr_y=t(data.frame(tr_ys))
rownames(tr_x)=NULL
rownames(tr_y)=NULL
tr_x=array(tr_x,c(nrow(tr_x),ncol(tr_x),1))
tr_y=array(tr_y,c(nrow(tr_y),ncol(tr_y),1))
return(list(tr_x,tr_y))
}
tr_=data_generate(ap_ts)
tr_x=tr_[[1]];tr_y=tr_[[2]]
dim(tr_x);dim(tr_y)
te_=data_generate(ap_ts_test)
te_x=te_[[1]];te_y=te_[[2]]
model=keras_model_sequential()
model$add(layer_input(shape=c(24,1)))
for(rate in rep(c(1,2,4,8),2)){
model$add(layer_conv_1d(filters=20,kernel_size = 2,padding='causal',
activation='relu',dilation_rate = rate))
}
model$add(layer_conv_1d(filters=1, kernel_size=1))
model%>%compile(
loss='mae',
optimizer='adam',
metrics=c('mae')
)
# remotes::install_github("r-tensorflow/wavenet")
library(wavenet)
model=wavenet(filters=64,kernel_size=3,
residual_blocks = 2^rep(1:8, 3), input_shape = list(24, 1),
output_channels = 1,output_activation = 'linear')
model %>%
keras::compile(
loss = 'mae',
optimizer = keras::optimizer_adam()
)
dim(tr_x);dim(tr_y)
model%>%fit(x = tr_x,y=tr_y,batch_size = 10,epochs = 1500)
n=9
dim(te_y)
ylim_=c(min(c((model%>%predict(te_x))[n,,1]),te_y[n,,1]),max(c((model%>%predict(te_x))[n,,1]),te_y[n,,1]))
plot((model%>%predict(te_x))[n,,1],type='l',ylim=ylim_);lines(te_y[n,,1],col=2)
예제 자료는 자료가 충분하지 않아서 성능이 좋지 못한 것 같다.
'통계 및 인공지능' 카테고리의 다른 글
[tensorflow]attention model (0) | 2021.06.11 |
---|---|
[tensorflow]Seq2Seq regression in R (0) | 2021.06.10 |
1D-CNN & Multi input Multi output Model in R (1) | 2021.05.02 |
R에서 ELMO 모형 사용하기 (0) | 2021.04.26 |
rTorch LSTM 예제 코드(GPU 사용) (0) | 2021.04.24 |