Skip to content

Instantly share code, notes, and snippets.

View vndee's full-sized avatar
🎯
Focusing

Duy Huynh vndee

🎯
Focusing
View GitHub Profile
## input image dimensions
img_x, img_y = 28, 28
## load Fashion-MNIST data set directly
(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
## reshape the data into a 4D tensor (sample_number, x_img, y_img, num_channels)
## there is only 1 channel in Fashion-MNIST
x_train = x_train.reshape(x_train.shape[0], img_x, img_y, 1)
x_test = x_test.reshape(x_test.shape[0], img_x, img_y, 1)
## normalize data
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
# define CNN (LeNet)
model = Sequential()
## layer 1
model.add(Conv2D(32, kernel_size = (5, 5), strides = (1, 1),
activation = 'relu', input_shape = input_shape))
model.add(MaxPooling2D(pool_size = (2, 2), strides = (2, 2)))
## layer 2
model.add(Conv2D(64, (5, 5), activation = 'relu'))
model.compile(loss = keras.losses.categorical_crossentropy,
optimizer = keras.optimizers.Adam(), metrics = ['accuracy'])
# training model
model.fit(x_train, y_train, batch_size = batch_size,
epochs = epochs,
verbose = 1,
validation_data = (x_test, y_test),
callbacks = [history])
# logging metrics
class AccuracyHistory(keras.callbacks.Callback):
def on_train_begin(self, logs = {}):
self.acc = []
def on_epoch_end(self, batch, logs = {}):
self.acc.append(logs.get('acc'))
history = AccuracyHistory()
@vndee
vndee / bc_01_01.py
Last active February 23, 2023 04:23
import json
import hashlib
import datetime
class Blockchain(object):
def __init__(self):
self.chain = []
@vndee
vndee / bc_01_02.py
Last active February 23, 2023 04:24
class Blockchain(object):
def __init__(self):
"""
Initializes the Blockchain
"""
self.chain = []
genesis_block = self.new_block(previous_hash='0', nonce=0)
# Mining first block (genesis block)
self.chain.append(self.proof_of_work(genesis_block))
@staticmethod
def hash(block):
"""
Creates a SHA-256 hash of a Block
:param block: block data
:return:
"""
block_string = json.dumps(block, sort_keys=True).encode()
return hashlib.sha256(block_string).hexdigest()
def proof_of_work(self, last_nonce):
"""
Simple Proof of Work Algorithm:
:param last_nonce: the last nonce
:return:
"""
nonce = 0
while self.check_valid_nonce(last_nonce, nonce) is False:
nonce += 1