JH721 SW자율차 [DeepLearning, CNN] //9주차-5

2051 단어 CNNCNN

DNN

from numpy import array, newaxis, random
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LSTM

x = array([[i*j for i in range(1,10)] for j in range(2,10)])
y = array([i+2 for i in range(8)])
print(x.shape)
print(x)
print(y.shape)
print(y)

x = x[..., newaxis]
print(y.shape)

model = Sequential()
model.add(LSTM(20, activation = 'relu', input_shape=(9,1))) # LSTM : numOfCell -> 4*n*(n+2)
model.add(Dense(5))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')


from tensorflow.keras.callbacks import EarlyStopping
early_stopping = EarlyStopping(monitor='loss', patience=3)
model.summary()
model.fit(x,y, epochs=1000, batch_size=1, verbose=2, callbacks=[early_stopping])
x_input = array([(i+1)*21 for i in range(9)]).reshape(1,9,1)
print(x_input)
y_hat = model.predict(x_input)
print(y_hat)

we ar looking for memory control, we dont need to know how to work

virtual

download virtualenv : sudo -H pip3 install virtualenv
virtualenv (tf1: dirctory name)
source tf1/bin/activate

character detect

import tensorflow as tf
import numpy as np
unique = 'helo'
y_data = [1,2,2,3]
x_data = np.array([[1,0,0,0], [0,1,0,0], [0,0,1,0], [0,0,1,0]], dtype='f')
cells = tf.nn.rnn_cell.BasicRNNCell(4)
state = tf.zeros([1, cells.state_size])
x_data = tf.split(x_data, 4)

outputs, state = tf.nn.static_rnn(cells, x_data, state)
logits = tf .reshape(outputs, [-1, 4])
targets = tf.reshape(y_data, [-1])
weights = tf.ones([4])

loss = tf.contrib.legacy_seq2seq.sequence_loss_by_example([logits], [targets], [weights])
cost = tf.reduce_sum(loss)
train_op = tf.train.RMSPropOptimizer(0.01,0.9).minimize(cost)

with tf.Session() as sess:
    tf.global_variables_initializer().run()
    for i in range(100):
        sess.run(train_op)
        r0,r1,r2,r3 = sess.run(tf.argmax(logits, 1))
        print(r0,r1,r2,r3, ':', unique[r0], unique[r1], unique[r2], unique[r3])

좋은 웹페이지 즐겨찾기