This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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')) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
model.compile(loss = keras.losses.categorical_crossentropy, | |
optimizer = keras.optimizers.Adam(), metrics = ['accuracy']) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# training model | |
model.fit(x_train, y_train, batch_size = batch_size, | |
epochs = epochs, | |
verbose = 1, | |
validation_data = (x_test, y_test), | |
callbacks = [history]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import hashlib | |
import datetime | |
class Blockchain(object): | |
def __init__(self): | |
self.chain = [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |