<jupyter_start><jupyter_text>高级API<jupyter_code>X_train, X_test, y_train,  y_test = train_test_split(train_data, 
                                                     train_label, test_size=0.2, 
                                                     random_state=42, stratify=train_label)
def conv_net2(X,n_classes, dropout, reuse, is_training):
    with tf.variable_scope("ConvNet", reuse=reuse):
        X = tf.cast(X, tf.float32)
        x = tf.reshape(X, shape=[-1, 28, 28, 1])
        conv1 = tf.layers.conv2d(x, 1, 5, activation=tf.nn.relu)
        conv1 = tf.layers.max_pooling2d(conv1, 2, 2)
        
        fc1 = tf.contrib.layers.flatten(conv1)
        fc1 = tf.layers.dense(fc1, 100)
        fc1 = tf.layers.dropout(fc1, rate=dropout, training=is_training)
        
        out = tf.layers.dense(fc1, 10)
    
    return out
def model_fn(features, labels, mode):
    logits_train = conv_net2(features, num_classes, dropout, reuse=False, is_training=True)
    logits_test = conv_net2(features, num_classes, dropout, reuse=True, is_training=False)
    
    pred_classes = tf.argmax(logits_test, axis=1)
#     pred_proba = tf.nn.softmax(logits_test)
    
    if mode == tf.estimator.ModeKeys.PREDICT:
        return tf.estimator.EstimatorSpec(mode, predictions=pred_classes)
    
    loss_op = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits_train, 
                                                                           labels=tf.cast(labels, dtype=tf.int32)))
    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
    train_op = optimizer.minimize(loss_op, global_step=tf.train.get_global_step())
    
    acc_op = tf.metrics.accuracy(labels=labels, predictions=pred_classes)
    
    estim_specs = tf.estimator.EstimatorSpec(mode=mode,predictions=pred_classes, 
                                        loss=loss_op, train_op=train_op, 
                                        eval_metric_ops={"accuracy":acc_op})
    return estim_specs
model = tf.estimator.Estimator(model_fn)
input_fn = tf.estimator.inputs.numpy_input_fn(x=X_train.values,
                                              y=y_train.values, 
                                              batch_size=batch_size, 
                                             num_epochs=None, shuffle=True)
model.train(input_fn, steps=num_steps)
# 评估模型
input_fn = tf.estimator.inputs.numpy_input_fn(x = X_test.values, 
                                             y = y_test.values, 
                                             batch_size=batch_size, 
                                             shuffle=True)
model.evaluate(input_fn)
# 进行预测
input_fn = tf.estimator.inputs.numpy_input_fn(x = X_test.values,  
                                             shuffle=True)
y_pred = np.array( list( (model.predict(input_fn)) ) )<jupyter_output>INFO:tensorflow:Calling model_fn.
INFO:tensorflow:Done calling model_fn.
INFO:tensorflow:Graph was finalized.
INFO:tensorflow:Restoring parameters from /var/folders/j0/mlln6qvj2kl42rr7mg80lsxc0000gn/T/tmpq4y4grbl/model.ckpt-500
INFO:tensorflow:Running local_init_op.
INFO:tensorflow:Done running local_init_op.
{'fraction_non_alphanumeric': 0.09549492385786802, 'fraction_numerical': 0.017766497461928935, 'mean_word_length': 2.1186943620178043, 'pattern_counts': {'":': 1, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 23, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '640647', 'license': 'Apache-2.0', 'n_tokens_mistral': 1047, 'n_tokens_neox': 999, 'n_words': 140, 'path': 'digit_recognizer/06.ipynb', 'repo_name': 'yunshuipiao/sw_kaggle', 'size': '3152'}