LoginSignup
0
0

More than 5 years have passed since last update.

多層ニューラルネットワークの作成/①

Last updated at Posted at 2018-06-26
neuralnetwork.py

import tensorflow as tf
import matplotlib.pyplot as plt
import csv
import os
import numpy as np
import requests

birth_weight_file = 'birth_weight.csv'
# Download data and create data file if file does not exist in current directory
if not os.path.exists(birth_weight_file):
    birthdata_url = 'https://github.com/nfmcclure/tensorflow_cookbook/raw/master' \
                '/01_Introduction/07_Working_with_Data_Sources/birthweight_data/birthweight.dat'
    birth_file = requests.get(birthdata_url)
    birth_data = birth_file.text.split('\r\n')
    birth_header = birth_data[0].split('\t')
    birth_data = [[float(x) for x in y.split('\t') if len(x) >= 1]
                  for y in birth_data[1:] if len(y) >= 1]
    with open(birth_weight_file, "w") as f:
        writer = csv.writer(f)
        writer.writerows([birth_header])
        writer.writerows(birth_data)
        f.close()

# read birth weight data into memory
birth_data = []
with open(birth_weight_file, newline='') as csvfile:
    csv_reader = csv.reader(csvfile)
    birth_header = next(csv_reader)
    for row in csv_reader:
        birth_data.append(row)

birth_data = [[float(x) for x in row] for row in birth_data]

y_vals = np.array([x[8] for x in birth_data])

y_vals = np.arry(x[8] for x in birth_data)
このy_valsで毎回エラーが生じる。
IndexError: list index out of range
birth_dataの特徴量は全部で9つだからnp.array(x[8] … in birth_data)の所は間違ってないはずなのに。

→ 理由はファイルデータが一行ずつ改行されててそこもnp.array(x[8]...)で読み込んでたからだった。改行を消してみたらしっかりできた。

TensorFlowのcookbook
https://github.com/nfmcclure/tensorflow_cookbook/blob/master/06_Neural_Networks/06_Using_Multiple_Layers/06_using_a_multiple_layer_network.py

TensorFlow cookbookにも同じ風に書いてあるので違うところで間違ってる可能性があると思うんだけど全然わからない。対処法分かる方いたら教えてください。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0