I tried Python> autopep8

Operating environment


GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 14.04 LTS desktop amd64
TensorFlow v0.11
cuDNN v5.1 for Linux
CUDA v8.0
Python 2.7.6
IPython 5.1.0 -- An enhanced Interactive Python.
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4

Related http://qiita.com/7of9/items/872d80d2a1cc36b5a053

I tried autopep8 which was taught by @ shiracamus's Comment.

Installation

$sudo apt-get install python-autopep8

Pre-processing code

TensorFlow code in progress

learn_in100out100.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import tensorflow as tf
import tensorflow.contrib.slim as slim
import numpy as np

'''
v0.1 Feb. 06, 2017
    - read [test_in.csv],[test_out.csv]
'''

'''
codingrule:PEP8
'''

filename_inp = tf.train.string_input_producer(["test_in.csv"])
filename_out = tf.train.string_input_producer(["test_out.csv"])
NUM_INP_NODE = 100
NUM_OUT_NODE = 100

# parse csv
# a. input node
reader = tf.TextLineReader()
key, value = reader.read(filename_inp)
deflist = [[0.] for idx in range(NUM_INP_NODE)]
input1 = tf.decode_csv(value, record_defaults=deflist)
# b. output node
key, value = reader.read(filename_out)
deflist = [[0.] for idx in range(NUM_OUT_NODE)]
output1 = tf.decode_csv(value, record_defaults=deflist)
# c. pack
inputs = tf.pack([input1])
outputs = tf.pack([output1])

batch_size = 1
inputs_batch, output_batch = tf.train.shuffle_batch([inputs, outputs], batch_size, capacity=1, min_after_dequeue=batch_size)

input_ph = tf.placeholder("float", [None, 1])
output_ph = tf.placeholder("float", [None, 1])

# network
hiddens = slim.stack(input_ph, slim.fully_connected, [7,7,7],
    activation_fn=tf.nn.sigmoid, scope="hidden")
prediction = slim.fully_connected(hiddens, 1, activation_fn=None, scope="output")
loss = tf.contrib.losses.mean_squared_error(prediction, output_ph)

train_op = slim.learning.create_train_op(loss, tf.train.AdamOptimizer(0.001))

init_op = tf.initialize_all_variables()

with tf.Session() as sess:
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    try:
        sess.run(init_op)
        for idx in range(10):
            inpbt, outbt = sess.run([inputs_batch, output_batch])
            _, t_loss = sess.run([train_op, loss], feed_dict={input_ph:inpbt, output_ph:outbt})

            if (idx+1) % 100 == 0:
            	print("%d,%f" % (idx+1, t_loss))
    finally:
        coord.request_stop()

processing

Since the processing result is output as standard, it was saved with an appropriate file name.

$autopep8 learn_in100out100.py > res.py

Comparison

$ diff learn_in100out100.py res.py 
38c38,39
< inputs_batch, output_batch = tf.train.shuffle_batch([inputs, outputs], batch_size, capacity=1, min_after_dequeue=batch_size)
---
> inputs_batch, output_batch = tf.train.shuffle_batch(
>     [inputs, outputs], batch_size, capacity=1, min_after_dequeue=batch_size)
44,46c45,48
< hiddens = slim.stack(input_ph, slim.fully_connected, [7,7,7],
<     activation_fn=tf.nn.sigmoid, scope="hidden")
< prediction = slim.fully_connected(hiddens, 1, activation_fn=None, scope="output")
---
> hiddens = slim.stack(input_ph, slim.fully_connected, [7, 7, 7],
>                      activation_fn=tf.nn.sigmoid, scope="hidden")
> prediction = slim.fully_connected(
>     hiddens, 1, activation_fn=None, scope="output")
61c63,64
<             _, t_loss = sess.run([train_op, loss], feed_dict={input_ph:inpbt, output_ph:outbt})
---
>             _, t_loss = sess.run(
>                 [train_op, loss], feed_dict={input_ph: inpbt, output_ph: outbt})
63,64c66,67
<             if (idx+1) % 100 == 0:
<             	print("%d,%f" % (idx+1, t_loss))
---
>             if (idx + 1) % 100 == 0:
>                 print("%d,%f" % (idx + 1, t_loss))
67d69
< 

If the argument is a list, is the rule of fitting to the ( position excluded? (The part of tf.train.shuffle_batch () and _, t_loss = sess.run ())

Pep8 before and after processing

$ pep8 learn_in100out100.py 
learn_in100out100.py:38:80: E501 line too long (124 > 79 characters)
learn_in100out100.py:44:56: E231 missing whitespace after ','
learn_in100out100.py:44:58: E231 missing whitespace after ','
learn_in100out100.py:45:5: E128 continuation line under-indented for visual indent
learn_in100out100.py:46:80: E501 line too long (81 > 79 characters)
learn_in100out100.py:61:71: E231 missing whitespace after ':'
learn_in100out100.py:61:80: E501 line too long (95 > 79 characters)
learn_in100out100.py:61:88: E231 missing whitespace after ':'
learn_in100out100.py:64:13: E101 indentation contains mixed spaces and tabs
learn_in100out100.py:64:13: W191 indentation contains tabs
learn_in100out100.py:67:1: W391 blank line at end of file
$ pep8 res.py 
res.py:64:80: E501 line too long (80 > 79 characters)

Only one remains below.

            _, t_loss = sess.run(
                [train_op, loss], feed_dict={input_ph: inpbt, output_ph: outbt})

You can improve your coding by referring to the part processed by autopep8.

If you pass it through autopep8, it will not be good to use it as OK for the time being.

Recommended Posts

I tried Python> autopep8
I tried Python> decorator
I tried fp-growth with python
I tried scraping with Python
I tried Python C extension
[Python] I tried using OpenPose
I tried gRPC with Python
I tried scraping with python
I tried to touch Python (installation)
I tried web scraping with python.
I tried using Thonny (Python / IDE)
I tried Grumpy (Go running Python).
I tried running prolog with python 3.8.2.
I tried Line notification in Python
I tried SMTP communication with Python
[Python] I tried using YOLO v3
I tried PyQ
I started python
I tried AutoKeras
I tried papermill
I tried django-slack
I tried Django
I tried spleeter
I tried cgo
I tried to summarize Python exception handling
I tried to implement PLSA in Python
I tried to implement permutation in Python
I tried scraping Yahoo News with Python
I tried to implement PLSA in Python 2
Python3 standard input I tried to summarize
I tried sending an email with python.
I tried using Bayesian Optimization in Python
I tried non-photorealistic rendering with Python + opencv
I tried using UnityCloudBuild API from Python
I tried to implement ADALINE in Python
I tried a functional language with Python
I tried recursion with Python ② (Fibonacci sequence)
I tried to implement PPO in Python
Python: I tried the traveling salesman problem
Wrangle x Python book I tried it [1]
Mayungo's Python Learning Episode 8: I tried input
[Python] I tried to calculate TF-IDF steadily
I tried scraping Yahoo weather (Python edition)
I tried to touch Python (basic syntax)
I tried the Python Tornado Testing Framework
#I tried something like Vlookup with Python # 2
[Python / DynamoDB / boto3] List of operations I tried
I tried "smoothing" the image with Python + OpenCV
I tried using argparse
I tried hundreds of millions of SQLite with python
I tried using anytree
I tried competitive programming
vprof --I tried using the profiler for Python
I tried web scraping using python and selenium
I tried "differentiating" the image with Python + OpenCV
I tried using aiomysql
I tried object detection using Python and OpenCV
I tried playing a typing game in Python
I tried using Summpy
I tried simulating the "birthday paradox" in Python
I tried the least squares method in Python