파일 데이터 로딩
본 글은 모두를 위한 머신러닝/딥러닝 강의를 참고하여 작성하였습니다.
소스 코드는 DeepLearningZeroToAll를 참고 하여 작성하였습니다.
ML lab 04-2: TensorFlow로 파일에서 데이터 읽어오기
이번 시간에는 로컬상에 저장 돼 있는 파일을 읽는 방법에 대해 알아보겠습니다.
사용할 데이터는 data-01-test-score.csv입니다.
import numpy as np
import tensorflow as tf
xy = np.loadtxt('data-01-test-score.csv', delimiter = ',', dtype = np.float32)
파일의 형식에 따라 데이터를 다르게 구분합니다.
오늘 쓰일 데이터는 csv파일로 값의 구분을 ,을 통해 이뤄집니다.
이때, 어떤 구분자를 이용해 구분할 것인지를 정해주는 것이 delimiter 입니다.
x_data = xy[:, 0:-1]
y_data = xy[:, [-1]]
print(x_data, "\nx_data shape:", x_data.shape)
print(y_data, "\ny_data shape:", y_data.shape)
[[ 73. 80. 75.]
[ 93. 88. 93.]
[ 89. 91. 90.]
[ 96. 98. 100.]
[ 73. 66. 70.]
[ 53. 46. 55.]
[ 69. 74. 77.]
[ 47. 56. 60.]
[ 87. 79. 90.]
[ 79. 70. 88.]
[ 69. 70. 73.]
[ 70. 65. 74.]
[ 93. 95. 91.]
[ 79. 80. 73.]
[ 70. 73. 78.]
[ 93. 89. 96.]
[ 78. 75. 68.]
[ 81. 90. 93.]
[ 88. 92. 86.]
[ 78. 83. 77.]
[ 82. 86. 90.]
[ 86. 82. 89.]
[ 78. 83. 85.]
[ 76. 83. 71.]
[ 96. 93. 95.]]
x_data shape: (25, 3)
[[152.]
[185.]
[180.]
[196.]
[142.]
[101.]
[149.]
[115.]
[175.]
[164.]
[141.]
[141.]
[184.]
[152.]
[148.]
[192.]
[147.]
[183.]
[177.]
[159.]
[177.]
[175.]
[175.]
[149.]
[192.]]
y_data shape: (25, 1)
tf.model = tf.keras.Sequential()
tf.model.add(tf.keras.layers.Dense(units = 1, input_dim = 3, activation = 'linear'))
tf.model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 1) 4
=================================================================
Total params: 4
Trainable params: 4
Non-trainable params: 0
_________________________________________________________________
tf.model.compile(loss = 'mse', optimizer = tf.keras.optimizers.SGD(lr = 1e-5))
history = tf.model.fit(x_data, y_data, epochs = 2000)
print("Your score will be ", tf.model.predict([[100, 70, 101]]))
print("Other scores will be ", tf.model.predict([[60, 70, 110], [90, 100, 80]]))
Your score will be [[185.7961]]
Other scores will be [[186.12904]
[174.52814]]
댓글남기기