Passed
Push — master ( ef01f2...fd3757 )
by Simon
01:46 queued 10s
created

Tensorflow   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 103
Duplicated Lines 65.05 %

Importance

Changes 0
Metric Value
wmc 3
eloc 70
dl 67
loc 103
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B cnn() 67 67 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
from __future__ import division, print_function, absolute_import
2
import tensorflow as tf
3
from tensorflow.examples.tutorials.mnist import input_data
4
5
from hyperactive import Hyperactive
6
7
mnist = input_data.read_data_sets("/tmp/data/", one_hot=False)
8
9
learning_rate = 0.001
10
num_steps = 500
11
batch_size = 128
12
13
num_input = 784
14
num_classes = 10
15
dropout = 0.25
16
17
X_train = mnist.train.images
18
y_train = mnist.train.labels
19
20
X_test = mnist.test.images
21
y_test = mnist.test.labels
22
23
24 View Code Duplication
def cnn(para, X_train, y_train):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
25
    def conv_net(x_dict, n_classes, dropout, reuse, is_training):
26
        with tf.variable_scope("ConvNet", reuse=reuse):
27
            x = x_dict["images"]
28
            x = tf.reshape(x, shape=[-1, 28, 28, 1])
29
            conv1 = tf.layers.conv2d(x, para["filters_0"], 5, activation=tf.nn.relu)
30
            conv1 = tf.layers.max_pooling2d(conv1, 2, 2)
31
            conv2 = tf.layers.conv2d(conv1, para["filters_1"], 3, activation=tf.nn.relu)
32
            conv2 = tf.layers.max_pooling2d(conv2, 2, 2)
33
            fc1 = tf.contrib.layers.flatten(conv2)
34
            fc1 = tf.layers.dense(fc1, para["dense_0"])
35
            fc1 = tf.layers.dropout(fc1, rate=dropout, training=is_training)
36
            out = tf.layers.dense(fc1, n_classes)
37
38
        return out
39
40
    def model_fn(features, labels, mode):
41
        logits_train = conv_net(
42
            features, num_classes, dropout, reuse=False, is_training=True
43
        )
44
        logits_test = conv_net(
45
            features, num_classes, dropout, reuse=True, is_training=False
46
        )
47
48
        pred_classes = tf.argmax(logits_test, axis=1)
49
        # pred_probas = tf.nn.softmax(logits_test)
50
51
        if mode == tf.estimator.ModeKeys.PREDICT:
52
            return tf.estimator.EstimatorSpec(mode, predictions=pred_classes)
53
54
        loss_op = tf.reduce_mean(
55
            tf.nn.sparse_softmax_cross_entropy_with_logits(
56
                logits=logits_train, labels=tf.cast(labels, dtype=tf.int32)
57
            )
58
        )
59
        optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
60
        train_op = optimizer.minimize(loss_op, global_step=tf.train.get_global_step())
61
62
        acc_op = tf.metrics.accuracy(labels=labels, predictions=pred_classes)
63
64
        estim_specs = tf.estimator.EstimatorSpec(
65
            mode=mode,
66
            predictions=pred_classes,
67
            loss=loss_op,
68
            train_op=train_op,
69
            eval_metric_ops={"accuracy": acc_op},
70
        )
71
72
        return estim_specs
73
74
    model = tf.estimator.Estimator(model_fn)
75
76
    input_fn = tf.estimator.inputs.numpy_input_fn(
77
        x={"images": X_train},
78
        y=y_train,
79
        batch_size=batch_size,
80
        num_epochs=None,
81
        shuffle=True,
82
    )
83
    model.train(input_fn, steps=num_steps)
84
85
    input_fn = tf.estimator.inputs.numpy_input_fn(
86
        x={"images": X_test}, y=y_test, batch_size=batch_size, shuffle=False
87
    )
88
    e = model.evaluate(input_fn)
89
90
    return float(e["accuracy"])
91
92
93
search_config = {
94
    cnn: {
95
        "filters_0": [16, 32, 64],
96
        "filters_1": [16, 32, 64],
97
        "dense_0": range(100, 2000, 100),
98
    }
99
}
100
101
opt = Hyperactive(X_train, y_train)
102
opt.search(search_config, n_iter=20)
103