반응형




+tomcat7


-주의 ; tomcat6에서는 war 배포시 서블릿에 대한 정의를 tomcat6에서는 web.xml에서 지정해주어야 한다.
그러나, tomcat7에서는 web.xml이나 코드내에 @WebServlet 지정자 둘 중에 하나만 지정해야 한다. 중복시, 웹앱이 실행이 되지 않을 수 있다.
/usr/tomcat7/logs/catalina.날짜.log 파일을 열어보면 알 수 있다.

-CentOS에서 java는 오라클 사이트에서 따로 구하지 않아도, 그냥 yum에서 jdk7 버전 깔고, 환경 설정만 해주면 된다.

: /etc/profile에 다음의 스크립트를 추가
# java7
export JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk.x86_64
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib/*:.

# tomcat7
export CATALINA_HOME=/usr/tomcat7

export PATH=$PATH:$CATALINA_HOME/bin:$JAVA_HOME/bin

+ 시작 방식도 좀 다름
tomcat6에서는 service tomcat6 start/stop/restart 등으로 했으나, tomcat7에서는 

startup.sh start/stop  
shutdown.sh

아니면 서비스 스크립트 만들면 동일하게 사용 가능.



+ 서비스 데몬 등록 (부팅시 자동 시작)


# vi /etc/init.d/tomcat 를 실행하여 아래내용을 입력하자  (tomcat7 init.d script 검색)


#!/bin/sh
#
# Tomcat7 auto-start
#
# chkconfig: 2345 90 90
# description: Auto-starts tomcat7
# processname: tomcat7
# pidfile: /var/run/tomcat7.pid
case $1 in
start)
    sh /usr/tomcat7/bin/startup.sh
    ;;
stop)
    sh /usr/tomcat7/bin/shutdown.sh
    ;;
restart)  # 신뢰할 수 없으니 restart 는 사용하지 말 것.
    sh /usr/tomcat7/bin/shutdown.sh
    sh /usr/tomcat7/bin/startup.sh
    ;;
esac
exit 0   


chmod 755 /etc/init.d/tomcat
실행권한 설정

chkconfig --add tomcat


-참고; tomcat 서비스가 안 죽으면 java 프로세스를 죽이면 된다. 주의!!!!
위 restart 커맨드는 절대 신뢰하면 안 됨!!!!! 주의!!!! 가끔 포트를 잡은 상태로 서비스가 종료 될 수 있다. 서비스 시작이 address in use로 되어
실패될 수 있음 !!! 스크립트 변경 필요!!!!!



반응형



+ 톰캣/Java 프로세스 모니터링 감시 / 내려가면 다시 실행. respawn

# cat tomcat_respawn.sh

#!/bin/sh
#tomcat PID 추출
PID=`lsof -itcp:8080 | grep LISTEN | awk 'NR==1 {print $2}'`
NEEDRUN=false

if [ ${PID} > 0 ]
then
     wget -q http://localhost:8080/test/test.jsp
     if [ -f test.jsp ]
     then
          rm -f test.jsp
     else
          NEEDRUN =true
     fi
else
     NEEDRUN =true
fi

if [ ${NEEDRUN} == "true" ]
then
     service tomcat7 restart
fi

위 스크립트를 crontab에 등록한다.
#chkconfig 로 crond가 운영중인지 확인한다.

#crontab -e
또는
/etc/crontab을 수정
  •  * /2 * * * * /etc/tomcat_respawn.sh > /dev/null 2>&1
2분마다 실행됨.


+ 다른 스크립트 (참고)
#!/bin/sh

            while true ; do
                /bin/date
                echo 'checking tomcat'
                if /bin/ps -elf | /bin/grep 'java' | /bin/grep 'j2sdk' then
                :
                echo 'WAS GOOD'
                else
                echo 'WAS BAD'
                /usr/local/tomcat/bin/startup.sh
                fi
                sleep 60
            done



반응형

시스템에서 타임존이 맞지 않을 때 강제로 설정하는 방법


+시간 타임존 timezone 문제

CentOS

1. 시스템 및 DB 시간
cp /usr/share/zoneinfo/Asia/Seoul /etc/localtime

# date()
=>KST

DB>select @@system_time_zone;
=>System time zone : KST

2. Tomcat 시간 설정
/etc/init.d/tomcat7 스크립트에 아래 내용 추가
export JAVA_OPTS="-Duser.timezone=GMT+09:00"

catalina.sh에 아래 추가
JAVA_OPTS="$JAVA_OPTS -Duser.timezone=GMT+09:00"



반응형


+tomcat의 구동 스크립트에 환경변수를 추가하여 java 메모리튜닝 및 외부 라이브러리 경로 설정을 한다.


구동 스크립트 경로: /usr/tomcat7/bin/catalina.sh

위 스크립트 가장 앞 부분에 추가한다.
보통 외부 라이브러리 경로는 /usr/local/lib에 추가한다. 그곳에 *.so 동적 라이브러리들을 추가하고 아래와 같이 LD_LIBRFARY_PATH에 추가해 준다.

#!/bin/sh

export LD_LIBRARY_PATH=.:/usr/lib/oracle/12.1/client64/lib:/usr/lib:/usr/local/lib
export CLASSPATH=/usr/java/latest/jre/lib/ext:/usr/java/latest/lib/tools.jar
JAVA_OPTS="-Dhttps.protocols=TLSv1,TLSv1.1,TLSv1.2 -Djava.library.path=/usr/local/lib -Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms1024m -Xmx1024m -XX:NewSize=512m -XX:MaxNewSize=512m -XX:PermSize=512m -XX:MaxPermSize=512m -XX:+DisableExplicitGC"

export TOMCAT_HOME=/usr/tomcat7
export CATALINA_HOME=/usr/tomcat7
export CATALINA_BASE=/usr/tomcat7
export CATALINA_PID=/usr/tomcat7/bin/tomcat.pid




+ 톰캣 튜닝
# vi /usr/tomcat/conf/server.xml


connectionTimeout="5000" 
     타임아웃으로 기본 60초 (60000) 이다. 10초이내 권장
maxThreads="100"
     tomcat의 최대 쓰레드 수. 최대 접속 가능한 active user수
acceptCount="100"
     tomcat thread full일 경우 대기 queue의 길이
disableUploadTimeout="true"
     데이터 업로드 할 때 사이즈가 크면 timeout이 걸릴수 있음. 이를 방지.
maxConnection="8192"
     tomcat이 유지하는 최대 접속 수.
     


-listner 설정
루트 계정으로 실행 못하게 막기
 <Listener className="org.apache.catalina.security.SecurityListener" checkedOsUsers="root" /> 

-Connector 설정
protocol="org.apache.coyote.http11.Http11Protocol"
acceptCount="10"
enableLookups="false"
compression="off"
maxConnection="8192"
maxKeepAliveRequest="1"
maxThread="100"
tcpNoDelay="true"

예)
<Connector port="8080" address="localhost" maxThreads="250" maxHttpHeaderSize="8192" emptySessionPath="true" protocol="HTTP/1.1" enableLookups="false" redirectPort="8181" acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="UTF-8" />


       <Connector port="8443" protocol="HTTP/1.1"
                SSLEnabled="true" scheme="https" secure="true"
                clientAuth="false" sslProtocol="TLS"
                keystoreFile="/var/www/tomcat.keystore" keystorePass="패스워드"
                URIEncoding="UTF-8"
                maxThreads="250" maxHttpHeaderSize="8192"
                enableLookups="false" acceptCount="100" connectionTimeout="20000"
                disableUploadTimeout="true" 
     compression="off"
     tcpNoDelay="true"
/>


+JVM 튜닝
vi $CATALINA_HOME/bin/catalina.sh
첫 줄에 JAVA_OPTS 추가.
-Xmx1024m –Xms1024m -XX:MaxNewSize=384m -XX:MaxPermSize=128m

메모리 부족시 덤프를 뜨게하여 추적할 수 있다.
-XX:-HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./java_pid<pid>.hprof
-XX:ParallelGCThreads=2 -XX:-UseConcMarkSweepGC

예)
JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms1024m -Xmx1024m -XX:NewSize=512m -XX:MaxNewSize=512m -XX:PermSize=512m -XX:MaxPermSize=512m -XX:+DisableExplicitGC"



+++ OS 튜닝 필요

+ 사용자 최대 파일(소켓 포함) 오픈 개수
ulimit -a로 확인 가능
소켓도 파일 이므로 최대 사용 개수가 제한된다....

:변경 방법
#sysctl -w fs.file-max=372738

아래도 작업
#vi /etc/sysctl.conf
fs.file-max=372738

#sysctl -p

; 변경 확인
sysctl fs.file-max
or
cat /proc/sys/fs/file-max

-최대 파일 오픈 개수 확인 ; ulimit -a
open files ; 1024
>변경방법
#vi /etc/security/limits.conf
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535
; xxxx는 65535 등...
ulimit -a 로 확인

-backlog 최대값 ; sys/socket.h
SOMAXCONN 값을 참조
>변경 방법
sysctl -a | grep somaxconn     ; 확인
sysctl -w net.core.somaxconn=2048



'Develop > Java' 카테고리의 다른 글

tomcat 프로세스 모니터링. 죽으면 다시 시작  (0) 2018.03.06
CentOS, tomcat 타임존 문제  (2) 2018.03.06
jqGrid(2) row(줄) 색상 변경/값 변경  (0) 2016.01.22
jqGrid (1)  (0) 2016.01.19
한글 인코딩 UTF8  (0) 2015.06.02
반응형

리눅스에서 간단히 OpenSSL로 암복호화 테스트하기


+인코딩

-base64 인코딩하기.

echo "this is a test" | openssl enc -base64

-base64 디코딩하기

echo "dGhpcyBpcyBhIHRlc3QK" | openssl enc -d -base64
this is a test

echo "dGhpcyBpcyBhIHRlc3QK" | openssl enc -base64 -d

this is a test



+ AES 256 암복호화

echo "this is a test" | openssl enc -aes-256-cbc -a

암호키 입력

출력은 base64 인코딩형태 (커맨드 마지막에 -a를 빼면 바이너리로 출력하므로 파일 리다이렉션> 으로 파일로 저장할 수도 있다.)


echo "base64 암호화값" | openssl enc -aes-256-cbc -d -a

암호키 입력


+기타 옵션

-in ; 입력 파일

-out ; 출력 파일

-a ; base64 인코딩하여 출력

-k ; 패스워드 (암복호화시 생략하면 프롬프트상에서 물어본다.)



+파일 암복호화 

$ openssl enc -aes-256-cbc -salt -a -in file.txt -k school -out file.txt.enc

$ cat file.txt.enc

U2FsdGVkX19VTO6a11weGV665ZPM3sGKpZadud21IEw=


$ openssl enc -aes-256-cbc -d -a -in file.txt.enc -out file.dec

enter aes-256-cbc decryption password:

$ cat file.dec

hello

(-a를 빼면 바이너리로 데이터 생성, -k 로 패스워드 지정가능.)




+ 해시 테스트

MD5 해시값을 생성한다. 파라미터로 파일명을 주면 파일을 읽어 해시값을 생성하고, echo로 출력한 것을 pipe로 받아 해시값을 생성할 수 도 있다. 

$ openssl md5 filename

$ echo "hello" | openssl md5

주의! echo로 출력시에는 마지막에 \n이 자동으로 들어간다.

이를 막으려면 -n 옵션을 echo에 추가해 주어야 한다.


$ openssl sha1 filename

$ openssl sha256 filename








'Security' 카테고리의 다른 글

DLKM 방식의 후킹 모듈 보호 메커니즘  (1) 2015.06.07
버퍼오버런(Buffer-Overrun)의 실체와 분석  (0) 2015.06.06
해시(Hash)  (1) 2015.06.02
반응형

아래와 같은 함수를 x가 -500~500 까지인 샘플을 가지고, 학습하여 -1000에서 1000까지의 출력결과를 보자.

학습은 기존의 샘플에 맞는 최적화된 모델을 찾을 뿐 그것으로 미래를 예측하는 것은 이 방식으로는 한계가 있다. 주어진 범위 내에서만 가능하다. 학습된 범위 밖의 영역에서는 정확하지 않다. 물론 가까운 정도는 어느정도 맞지 않을까.

학습할 그래프.

위 그래프의 수식은 아무렇게나 만들어본 

y=sin(x/100)*500+ cos(x/27)*300 + x


# -*- coding: utf-8 -*-


'''

Created on Sat May 13 10:54:59 2017


@author: Junhee

'''


#

# complex algbra predict learning? is it possible???

# continous valued target bounded range (-1000,1000)

# not using polynomial modified input!. because I don't know

# what polynomial degree is correct. 

#



import numpy as np

import tensorflow as tf

import matplotlib.pyplot as plt

import time



bDrawFigure = True

# train yes or no

bTrain = 1

#learning_rate = 0.1


#learning_rate = 0.00001

learning_rate = 0.001

train_cnt = 4000




# unknown algbra function

def problem(x):

    y = np.sin(x/100)*500 + np.cos(x/27)*300 +x

    return y

# can u imagine? i dont know.

xrange = np.linspace(-500, 500, 1000)

y_correct = problem(xrange)



# draw graph?

if bDrawFigure:

    plt.figure()

    plt.plot(xrange, y_correct)


output_space = 2000     # tanh ; -1~1 X output_space ; output range! ; -2000~2000


                    # x domain ; -1000~1000

trainx = np.random.rand(train_cnt)*2000-1000   # training set must be shuffle!!!

trainx = trainx.reshape([-1,1])

trainy_cor = problem(trainx)

trainy_cor = trainy_cor.reshape([-1,1])



X = tf.placeholder(tf.float32, shape=[None,1])

Y = tf.placeholder(tf.float32, shape=[None,1])


# make network NN

W1 = tf.Variable(tf.random_normal([1,512]))

b1 = tf.Variable(tf.zeros([512]))

L1 = tf.nn.sigmoid(tf.matmul(X, W1)+b1)

#L1 = tf.nn.tanh(tf.matmul(X, W1)+b1) 


W2 = tf.Variable(tf.random_normal([512,1024]))

b2 = tf.Variable(tf.zeros([1024]))

L2 = tf.nn.sigmoid( tf.matmul(L1, W2)+b2) 


W3 = tf.Variable(tf.random_normal([1024,1024]))

b3 = tf.Variable(tf.zeros([1024]))

L3 = tf.nn.sigmoid( tf.matmul(L2, W3)+b3) 


W4 = tf.Variable(tf.random_normal([1024,1024]))

b4 = tf.Variable(tf.zeros([1024]))

L4 = tf.nn.sigmoid( tf.matmul(L3, W4)+b4) 


W5 = tf.Variable(tf.random_normal([1024,1]))

b5 = tf.Variable(tf.zeros([1]))

#Llast = tf.nn.tanh( tf.matmul(L4, W5)+b5) *output_space

#Llast = (tf.matmul(L4, W5)+b5) *output_space

Llast = (tf.matmul(L4, W5)+b5)


cost = tf.reduce_mean( tf.square(Llast-Y) )


train = tf.train.AdamOptimizer(learning_rate).minimize(cost)


sess = tf.Session()

sess.run( tf.global_variables_initializer())


param_list = {'W1':W1,'b1':b1,

              'W2':W2,'b2':b2,

              'W3':W3,'b3':b3,

              'W4':W4,'b4':b4,

              'W5':W5,'b5':b5,

              }

saver = tf.train.Saver(param_list)

#saver.restore(sess, './unknown_predict.ptl')    # continue train


print( "bTrain=", bTrain, " train_cnt=", train_cnt)

# train

oldtime = time.time()

if bTrain:

    for i in range(train_cnt) :

        _, _cost = sess.run( [train, cost], feed_dict={X:trainx, Y:trainy_cor})

        if i%100==(100-1):

            newtime = time.time()

            term = newtime-oldtime

            print( "train=",i," cost=",_cost, " remaintime=", ((train_cnt-i)/100+1)*term)

            oldtime=newtime

    saver.save(sess, './unknown_predict.ptl')


##########################################################

# test

saver.restore(sess, './unknown_predict.ptl')


test_cnt = 500

testx = np.random.rand(test_cnt)*1000-500  # training set must be shuffle!!!

testx = testx.reshape([-1,1])

testy_cor = problem(testx)

testy_cor = testy_cor.reshape([-1,1])

_llast = sess.run( Llast, feed_dict = { X:testx } )

testcost = np.mean( np.square(_llast-testy_cor))

print ( "cost=", testcost )

#for i in range(test_cnt):

#    print("input=", testx[i], " predict=", _llast[i], " correct=", testy_cor[i])

    

#draw compare

if bDrawFigure:

#    xrange = np.linspace(-1000, 1000, 2000)

    xrange = np.linspace(-500, 500, 1000)

    xrange = xrange.reshape(-1, 1)

    _llast2 = sess.run( Llast, feed_dict = { X:xrange } )

    xrange = xrange.reshape(-1)

    testyrange = _llast2.reshape(-1)

    plt.plot(xrange, testyrange, c='r')    

    

plt.show()



GPU로 돌려야지 안 그럼 오래걸린다.

어쨋거나 결과는 아래와 같다.



0부근에서 오차가 심하다.

학습을 오래 할수록 좋아지기는 한다.




'AI(DeepLearning)' 카테고리의 다른 글

tensorflow 강좌2. 자료형  (0) 2018.07.16
[tf] tensorflow 강좌1 로딩테스트  (0) 2018.07.16
[tf] unknown math polynomial function modeling  (0) 2017.06.01
[tf] XOR tensorflow로 학습구현  (0) 2017.05.23
[tf] XOR manual solve  (0) 2017.05.23
반응형

알 수 없는 n차 다항 함수에 근사하는 샘플데이터를 학습시켜보자.

단순한 linear regression으로는 다차항 함수의 학습이 되지 않는다.

feature 값을 x^2, x^3, .. 이런식으로 변환하여 feture들을 늘려서 학습시킨다면 가능할수도 있지만, 몇 차로 해야될지도 알 수 없고 전처리를 해야되서 예측이 어렵다.

이것을 해결하기 위해서는 deep and wide한 네트웍을 구성하여 학습을 시킨다.

단, 주의해야될 점은 바로 activation function이다. 항등함수로만 하면 아무리 네트웍을 구성해도 학습이 되지 않는다.

네트웍을 affine  계층으로만 하게되면 weighted sum을 다시 weighted sum을 하게 되므로 다중 레이어를 둔 의미가 상실되게 된다. 즉, xw+b를 다시 (xw+b)w+b가 되서 아무리 반복해도 x*w+b꼴로 표현이 가능하다. 즉, 단층레이어와 동일할 뿐이다.

activation function은 다양한 정보 표현이 가능하도록 비선형 함수들을 사용해야 한다. sigmoid, tanh, relu 등.

그리고 마지막 계층 또한 중요하다. 1/0을 판단하는 binary classification이라면 sigmoid로 하고, multinomial classfication이라면 softmax를 사용하며, 실수범위의 예측이라면???  출력값의 범위가 표현될 수 있는 function을 만든다.  학습이 잘 되냐 실패하냐거 거의 여기에 달린 것 같다.

ex) sigmoid or tanh or identity 의 값에 * 도메인크기 + bias , 

(identity는 항등함수를 말함. 즉, weighted sum 인풋이 그대로 출력되는 함수) 

몇 번 테스트해보니 sigmoid나 tanh가 적당하다. 


단, 이렇게 학습하여 예측하는 것에는 한계가 있다.

학습한 데이터의 도메인에 해당되는 범위내에서는 예측가능하나, 학습되지 않은 구간에서는 맞지 않게 된다.


# -*- coding: utf-8 -*-


'''

Created on Sat May 13 10:54:59 2017


@author: Junhee

'''


#

# complex algbra predict learning? is it possible???

# continous valued target bounded range (-1000,1000)

# not using polynomial modified input!. because I don't know

# what polynomial degree is correct. 

#



import numpy as np

import tensorflow as tf

import matplotlib.pyplot as plt

import time



bDrawFigure = True

# train yes or no

bTrain = 1

#learning_rate = 0.1

#learning_rate = 0.0001

learning_rate = 0.00001

#learning_rate = 0.1

trainset_cnt = 200

epoch_cnt = 4000

#epoch_cnt = 40000





# unknown algbra function

def problem(x):

#    y = x**2

    y = x**2-30*x

    return y

# can u imagine? i dont know.

xrange = np.linspace(0, 100, 200)

y_correct = problem(xrange)


# draw graph?

if bDrawFigure:

    plt.figure()

    plt.plot(xrange, y_correct)


output_space = 40000     # tanh ; -1~1 X output_space ; output range! ; -2000~2000


                    # x domain ; -1000~1000

trainx = np.random.rand(trainset_cnt)*100-0   # training set must be shuffle!!!

trainx = trainx.reshape([-1,1])

trainy_cor = problem(trainx)

trainy_cor = trainy_cor.reshape([-1,1])



X = tf.placeholder(tf.float32, shape=[None,1])

Y = tf.placeholder(tf.float32, shape=[None,1])


# make network NN

W1 = tf.Variable(tf.random_normal([1,512]))

b1 = tf.Variable(tf.zeros([512]))

L1 = tf.nn.sigmoid(tf.matmul(X, W1)+b1)

#L1 = tf.nn.tanh(tf.matmul(X, W1)+b1) 


W2 = tf.Variable(tf.random_normal([512,1024]))

b2 = tf.Variable(tf.zeros([1024]))

L2 = tf.nn.sigmoid( tf.matmul(L1, W2)+b2) 


W3 = tf.Variable(tf.random_normal([1024,1024]))

b3 = tf.Variable(tf.zeros([1024]))

L3 = tf.nn.sigmoid( tf.matmul(L2, W3)+b3) 


W4 = tf.Variable(tf.random_normal([1024,1024]))

b4 = tf.Variable(tf.zeros([1024]))

L4 = tf.nn.sigmoid( tf.matmul(L3, W4)+b4) 


W5 = tf.Variable(tf.random_normal([1024,1]))

b5 = tf.Variable(tf.zeros([1]))

#Llast = tf.nn.tanh( tf.matmul(L4, W5)+b5) *output_space


#good

Llast = (tf.nn.sigmoid( tf.matmul(L4, W5)+b5)) *output_space - 500


#Llast = (tf.matmul(L4, W5)+b5) *output_space


# bad... 

#Llast = (tf.matmul(L4, W5)+b5)


cost = tf.reduce_mean( tf.square(Llast-Y) )


train = tf.train.AdamOptimizer(learning_rate).minimize(cost)


sess = tf.Session()

sess.run( tf.global_variables_initializer())


param_list = {'W1':W1,'b1':b1,

              'W2':W2,'b2':b2,

              'W3':W3,'b3':b3,

              'W4':W4,'b4':b4,              

              'W5':W5,'b5':b5,

              }

saver = tf.train.Saver(param_list)

#saver.restore(sess, './unknown_predict_poly.ptl')    # continue train


print( "bTrain=", bTrain, " trainset_cnt=", trainset_cnt, " epoch=cnt", epoch_cnt)

# train

oldtime = time.time()

if bTrain:

    for i in range(epoch_cnt) :

        _, _cost = sess.run( [train, cost], feed_dict={X:trainx, Y:trainy_cor})

        if i%100==(100-1):

            newtime = time.time()

            term = newtime-oldtime

            print( "train=",i," cost=",_cost, " remaintime=", ((epoch_cnt-i)/100+1)*term)

            oldtime=newtime

    saver.save(sess, './unknown_predict_poly.ptl')


##########################################################

# test

saver.restore(sess, './unknown_predict_poly.ptl')

    

#draw compare

if bDrawFigure:

#    xrange = np.linspace(-1000, 1000, 2000)

    xrange = np.linspace(1, 200, 200)

    xrange = xrange.reshape(-1, 1)

    _llast2 = sess.run( Llast, feed_dict = { X:xrange } )

    xrange = xrange.reshape(-1)

    testyrange = _llast2.reshape(-1)

    plt.plot(xrange, testyrange, c='r')    

    

plt.show()



출력결과

bTrain= 1  trainset_cnt= 200  epoch=cnt 4000

train= 99  cost= 4.10605e+07  remaintime= 408.33133206844326

train= 199  cost= 6.47534e+06  remaintime= 374.9750850892067

train= 299  cost= 3.22122e+06  remaintime= 390.8726796555519

...

train= 3399  cost= 12447.5  remaintime= 73.77905233621597

train= 3499  cost= 12130.0  remaintime= 62.91447646141052

train= 3599  cost= 11828.8  remaintime= 51.30402812004089

train= 3699  cost= 11539.5  remaintime= 43.085111978054044

train= 3799  cost= 11257.4  remaintime= 33.96168860197067

train= 3899  cost= 10978.7  remaintime= 21.201974751949308

train= 3999  cost= 10699.7  remaintime= 11.279016330242158

INFO:tensorflow:Restoring parameters from ./unknown_predict_poly.ptl

초반에 cost가 어마어마하다. 학습이 잘 된다면 위와 같이 점점 줄어들 것이다.


임의의 다차항 함수의 샘플을 생성하여 학습한다.

y=x^2-30x 라는 함수를 배워본다. 단, 전체 실수범위에 대해서 학습은 불가능하다.

샘플의 범위는 유한하기 때문. x의 범위를 0~100까지를 학습해 본다.

학습데이터의 모습은 아래와 같다.

이를 학습하여 200까지 예측해 보면, 100~200 범위는 배우지 않았기 때문에 오차가 커진다.

그러나 학습한 범위인 0~100까지는 거의 정확하다!

최종 학습 결과.

위 그림에서 0~100 구간은 파란선과 빨간선의 거의 일치한다.


추신)

샘플에 noise를 가해주고 학습하는게 좋을 것 같다.

학습시에는 batch로 샘플데이터를 random select하게 해야 학습이 더 잘 될 것 같다. 

다른 복잡한 곡선도 테스트해 보자.


반응형


보간법. interpolate 


몇 개의 샘플 포인트들로 추정하여 인접한 점들 사이를 다항식 함수로 완만한 곡선으로 이어준 것을 스플라인 곡선이라고 한다.

보간법은 이러한 미싱 포인트(추정)들을 계산/추정하는 방법이다.

간단하게는 linear 방식(1차)이 있고, 2차, 3차, 4차 곡선등으로 확장하면서 보다 다양하게 추정할 수 있다.



위 그림은 파란색 점의 샘플 좌표들만을 가지고, 스플라인 곡선을 만들어 그린 추정 그래프이다.

python에서 scipy 라이브러리의 interpolate를 사용하여 구현하였다.

아래 코드에서 splrep에 별다른 옵션이 없으면 3차 스플라인 곡선으로 추정하게 된다. (k=1(선형보간), k=2, k=3(default), k=4 이런식으로 옵션을 준다. )

[python code]


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu May 25 2017
@author: crazyj
"""

# interpolation test

import numpy as np
import scipy.interpolate as ip
from scipy.interpolate import splrep, splev
import matplotlib.pyplot as plt

# 0~10까지 15개로 나누어 점을 찍음.
x0 = np.linspace(0, 10, 15)
print('x0=', x0)
# cosine 값을 계산
y0 = np.cos(x0)

# x, y (샘플)값을 주고 추정하는 스플라인 곡선을 만든다.
spl = splrep(x0, y0)
# 0~10까지 50구간에 대한 모든 점들을 위 스플라인 곡선으로 추정한 y값을 구한다.
x1 = np.linspace(0, 10, 50)
y1 = splev(x1, spl)

# 그린다.
plt.figure(figsize=(16, 5))
plt.subplot(121)
plt.plot(x0, y0, 'o')
plt.plot(x1, y1, 'r')
plt.grid()

# 이번에는 sine 곡선으로 추정해 본다.
plt.subplot(122)
y2=np.sin(x0)
spl2=splrep(x0, y2)
y3=splev(x1, spl2)
plt.plot(x0, y2, 'o')
plt.plot(x1, y3, 'b')
plt.grid()
plt.show()



'Python' 카테고리의 다른 글

Python 강좌4 정규식 regular expression  (0) 2018.03.19
Python 강좌3 자료형. 수/문자열  (1) 2018.03.16
Python 강좌2 if/for/while/function  (0) 2018.03.15
Python 강좌1. 산술연산, range  (0) 2018.03.14
Linear regression  (0) 2018.03.13
반응형


+ Tensorflow로 XOR  학습을 구현하기


XOR 학습을 수동으로 계산하지 않고, tensorflow의 api를 사용하면 심플해진다.

복잡한 네트웍도 쉽게 구현이 가능하다.


노드의 개수 및 레이어를 2-4-1 로 구성.

입력층에 노드 2개(feature 개수. x1, x2)

은닉층에는 4개 

출력층은 1개의 노드. (Y)

back-propagation을 위한 골치 아픈 작업(미분)들을 할 필요없이 api 하나로 학습 가능! activation function도 원하는대로 쉽게 변경하고, 학습 알고리즘은 쉽게 바꿀 수 있다.


#!/usr/bin/env python3

# -*- coding: utf-8 -*-

"""

Created on Tue May 23 14:56:53 2017


@author: crazyj

"""



import numpy as np

import tensorflow as tf


# trainint set

X_train = np.array( [[0,0], [0,1], [1,0], [1,1]])

T_train = np.array( [[0], [1], [1], [0]] )



# placeholder

X = tf.placeholder(tf.float32, [None, 2])

T = tf.placeholder(tf.float32, [None, 1])


# variable

W1 = tf.Variable(tf.truncated_normal([2,4]))

b1 = tf.Variable(tf.zeros([4]))

W2 = tf.Variable(tf.truncated_normal([4,1]))

b2 = tf.Variable(tf.zeros([1]), dtype=tf.float32)


# model

A1 = tf.matmul(X, W1)+b1

Z1 = tf.sigmoid(A1)

A2 = tf.matmul(Z1, W2)+b2

Z2 = tf.sigmoid(A2)


learn_rate = 0.1

Cost = tf.reduce_mean(tf.reduce_sum(tf.square(Z2-T), 1))

train = tf.train.GradientDescentOptimizer(learn_rate).minimize(Cost)


predict = Z2


sess = tf.Session()

sess.run(tf.global_variables_initializer())

for i in range(5000):

    _train, _Cost = sess.run([train, Cost], feed_dict={X:X_train, T:T_train})

    print( "cost=", _Cost)

    

_predict = sess.run([predict], feed_dict={X:X_train})

print("predict=", _predict)

print("result=", np.array(np.array(_predict)>=0.5, np.int))




결과

cost= 0.021748

cost= 0.0217359

cost= 0.0217239

predict= [array([[ 0.10571096],

       [ 0.86153752],

       [ 0.84178925],

       [ 0.17739831]], dtype=float32)]

result= [[[0]

  [1]

  [1]

  [0]]]


+ 코드 설명

# trainint set

X_train = np.array( [[0,0], [0,1], [1,0], [1,1]])

T_train = np.array( [[0], [1], [1], [0]] )

훈련 데이터는 당연히 xor의 입력 조합에 따른 결과를 훈련시킨다. (0,0) -> 0 , (0,1) -> 1 , (1,0) -> 1 , (1,1)->1


# placeholder

X = tf.placeholder(tf.float32, [None, 2])

T = tf.placeholder(tf.float32, [None, 1])

플레이스 홀더는 텐서플로우내에서 돌아갈 입출력 변수들이라고 보면 된다. 노드에서 변수가 되는 것을 X 입력층은 (?,2) 매트릭스 크기(로우는 임의의 개수, 컬럼은 2개(x1,x2)), 출력층 T는 (n,1) 매트릭스 형태로 선언한다.


# variable

W1 = tf.Variable(tf.truncated_normal([2,4]))

b1 = tf.Variable(tf.zeros([4]))

W2 = tf.Variable(tf.truncated_normal([4,1]))

b2 = tf.Variable(tf.zeros([1]), dtype=tf.float32)

텐서 플로우 변수들을 정의한다. 이것은 그래프에 구성될 노드들의 변수의 형태를 정의한다.

네트웍을 2-4-1로 구성할 것이므로 여기에 따른 weight를 담을 변수와 bias 변수의 형태는 다음과 같다.

2개 노드는 placeholder로 X이고, 2-4연결되는 W(웨이트)는 2행(입력노드개수) 4열(출력노드개수) 매트릭스이다. bias는 4개(출력노드개수)이다.

4-1로 연결되는 파트의 W는 4x1 이렇게 b는 1개 이렇게 구성한다. 초기값들은 랜덤하게 채워준다. bias는 0으로 초기화해준다.


# model

A1 = tf.matmul(X, W1)+b1

Z1 = tf.sigmoid(A1)

A2 = tf.matmul(Z1, W2)+b2

Z2 = tf.sigmoid(A2)

이제 빠진 히든 노드들과 출력층 노드들을 구성한다.

2-4-1네트웍에서 2개는 X, 4개는 A1으로 정하고,

A1 = X x W1 + b1 으로 정의한다.

Z1=은 A1에 활성화함수 sigmoid를 적용한다.


A2=Z1 x W2 + b2로 정의한다. 앞 노드의 출력 결과에 weighted sum이다.

Z2 = A2에 sigmoid를 적용한 것으로 최종 output이다.


learn_rate = 0.1

Cost = tf.reduce_mean(tf.reduce_sum(tf.square(Z2-T), 1))

train = tf.train.GradientDescentOptimizer(learn_rate).minimize(Cost)

predict = Z2

이제 학습방식을 정한다.
cost function은 오차 제곱법을 쓰고, 학습방식은 gradient descent를 사용하여 훈련 그래프를 만든다.
예측값은 최종 노드 출력인 Z2가 된다.


sess = tf.Session()

sess.run(tf.global_variables_initializer())

for i in range(5000):

    _train, _Cost = sess.run([train, Cost], feed_dict={X:X_train, T:T_train})

    print( "cost=", _Cost)

텐서플로우로 학습을 돌린다. 세션을 만들고, 초기화하여 5000번 학습한다. 그래프의 꼭지인 train을 집어 넣고, feed_dict로 플레이스 홀더 X, T에 훈련 데이터를 넣는다.  학습1회마다 cost값을 출력해 본다. 


_predict = sess.run([predict], feed_dict={X:X_train})
학습 완료후, 학습이 잘 되었는지  입력값 X_train을 넣고 출력값을 생성한다.

print("result=", np.array(np.array(_predict)>=0.5, np.int))

최종 결과로 시그모이드 함수에서 0.5이상이면 1로 미만이면 0으로 출력하여 binary 분류를 한다.





반응형


XOR  학습

단층 퍼셉트론으로는 비선형이 학습이 안된다.

따라서 멀티 퍼셉트론을 사용. 입력 레이어를 제외하고 Two-Layer 구성.

Sigmoid를 사용. 0/1 binary구별로 함.



#!/usr/bin/env python3

# -*- coding: utf-8 -*-

"""

Created on Tue May 23 14:10:10 2017


@author: crazyj

"""


import numpy as np

import os


# xor simple network.

#   X(2) - 2 - Y(1)

# sigmoid activation function use.

# manual gradient

#

# if fail?, try again!

#   local minima problem exists... 

#   make deep and wide network.

#



X = np.array( [[0,0], [0,1], [1,0], [1,1]])

T = np.array( [[0], [1], [1], [0]] )


np.random.seed(int(os.times()[4]))


W1 = np.random.randn(2,2)

b1 = np.zeros([2])

W2 = np.random.randn(2,1)

b2 = np.zeros([1])



def Sigmoid(X):

    return 1/(1+np.exp(-X))


def Predict(X,  W1, b1, W2, b2):

    Z1 = np.dot(X, W1)+b1

    A1 = Sigmoid(Z1)

    Z2 = np.dot(A1, W2)+b2

    A2 = Sigmoid(Z2)

    Y = A2

    return Y


def Cost(X, W1, b1, W2, b2, T):

    epsil = 1e-5

    Z1 = np.dot(X, W1)+b1

    A1 = Sigmoid(Z1)

    Z2 = np.dot(A1, W2)+b2

    A2 = Sigmoid(Z2)

    Y = A2

    return np.mean(-T*np.log(Y+epsil)-(1-T)*np.log(1-Y+epsil))



def Gradient(learning_rate, X, W1, b1, W2, b2, T):

    Z1 = np.dot(X, W1)+b1

    A1 = Sigmoid(Z1)

    Z2 = np.dot(A1, W2)+b2

    A2 = Sigmoid(Z2)

    deltaY = A2-T

    deltaA1 = np.dot(deltaY, W2.T) * (A1*(1-A1))

    m = len(X)

    

    gradW2 = np.dot(A1.T, deltaY)

    gradW1 = np.dot(X.T, deltaA1)

    W2 = W2-(learning_rate/m)*gradW2

    b2 = b2-(learning_rate/m)*np.sum(deltaY)

    W1 = W1-(learning_rate/m)*gradW1

    b1 = b1-(learning_rate/m)*np.sum(deltaA1)

    return (W1, b1, W2, b2)



for i in range(3000):

    J= Cost(X,W1,b1,W2,b2,T)

    W1,b1,W2,b2 = Gradient(1.0, X, W1, b1, W2, b2, T)

    print ("Cost=",J)


Y = Predict(X, W1, b1, W2, b2)

print("predict=", Y)




결과


Cost= 0.351125685078

predict= [[ 0.50057071]

 [ 0.49643107]

 [ 0.99648031]

 [ 0.00640712]]



실패?

다시 실행을 반복하다 보니 성공할때도 있다??? local minima 문제가 있음.

이를 해결하기 위해서는 여러번 시도해서 코스트가 낮아질 때까지 처음부터 반복(initialize 가 중요).하던가 network을 deep & wide하게 설계한다.


Cost= 0.00403719259697

predict= [[ 0.00475473]

 [ 0.99634993]

 [ 0.99634975]

 [ 0.00409427]]

이건 성공 결과.


+ Recent posts