import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
とするとハッピーになれるようでした。(下記サンプルコード転記)
import tensorflow.compat.v1 as tf
import numpy as np
tf.disable_v2_behavior()
# Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data * 0.1 + 0.3
# Try to find values for W and b that compute y_data = W * x_data + b
# (We know that W should be 0.1 and b 0.3, but TensorFlow will
# figure that out for us.)
W = tf.Variable(tf.random.uniform([1], -1.0, 1.0))
b = tf.Variable(tf.zeros([1]))
y = W * x_data + b
# Minimize the mean squared errors.
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)
# Before starting, initialize the variables. We will 'run' this first.
init = tf.global_variables_initializer()
# Launch the graph.
sess = tf.Session()
sess.run(init)
# Fit the line.
for step in range(201):
sess.run(train)
if step % 20 == 0:
print(step, sess.run(W), sess.run(b))
# Learns best fit is W: [0.1], b: [0.3]
eto = ["申","酉","戌","亥","子","丑","寅","卯","辰","巳","午","未"]
try:
x = input("生まれた年を西暦で入力してください。: ")
y = int(x)
y = y % 12
print("あなたの干支は {} です。".format(eto[y]))
except(ValueError):
print("4桁の半角数字を入力してください。")
Titles = [
"Discopolis 2.0 - Fehrplay Remix",
"Never Alone - J-Kraken Remix",
"Golden",
"Steal Your Heart",
"I Want You To Know",
]
Artists = [
"Lifelike, Kris Menace, Fehrplay",
"Chachi, Natascha Bessez",
"Thomas Hayes, Kyler England",
"BRKLYN, Lenachka",
"ゼッド, セレーナ・ゴメス",
]
i = 0
for i, new in enumerate(zip(Titles,Artists)):
new = Artists[i]
x = input("{} {} / {} | アーティスト名を修正してください。:".format(i, Titles[i], Artists[i]))
if x == "":
continue
elif x == "q":
break
new = x
Artists[i] = new
print(i, Titles[i], "/", Artists[i])
書き方として、import print_module as pm としておけば、main.py で外部関数をpm.Hello として読めるということです。
print_module.py
def Hello(name):
print("Hello {}!".format(name))
main.py
import print_module as pm
pm.Hello("World")
結果
Hello World!
事前に辞書ファイルを外部に用意して必要に応じて呼び出すようにすることも出来ますね。
music_list.py
Titles = [
"Atmosphere",
"I Need Your Love",
"Enigma",
"Addicted To A Memory",
]
Artists = [
"Husman",
"First State",
"Seth Hills",
"ゼッド",
]
music = dict(zip(Titles, Artists))
query.py
import music_list as mu
title = input("タイトルを入力してください。:")
if title in mu.music:
print(mu.music[title])
else:
print("見当たりません。")