初心者Tensorflow variables (変数)
今回は定数と変数を見ていこうと思います。
import tensorflow as tf var = tf.Variable(0)#変数定義 integer = tf.constant(3)#定数定義 adds = tf.add(var, integer)#adds = var + integer ans_var = tf.assign(var, adds)#ans_var = (var = adds) muls = tf.multiply(adds, ans_var)#muls = adds * ans_var with tf.Session() as sess: sess.run(tf.global_variables_initializer())#変数の初期化 print(sess.run([muls]))#Sessionの実行
console: 9
もう一度sees.run(muls)を行うと、
変数は最初3から始まるため、結果は36となる
import tensorflow as tf var = tf.Variable(0)#変数定義 integer = tf.constant(3)#定数定義 adds = tf.add(var, integer)#adds = var + integer ans_var = tf.assign(var, adds)#ans_var = (var = adds) muls = tf.multiply(adds, ans_var)#muls = adds * ans_var with tf.Session() as sess: sess.run(tf.global_variables_initializer())#変数の初期化 print(sess.run([muls]))#Sessionの実行 print(sess.run([muls]))#もう一度Sessionの実行
console: 9 36
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- -
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
Tensorflow初心者 初めの1歩 Addしてみる
tensorflow RUL:API Documentation | TensorFlow
import tensorflow as tf num1 = tf.constant(99) num2 = tf.constant(1) add = tf.add(num1, num2) with tf.Session() as sess: result = sess.run(add) print(result)
↑
結果は100です。。。。
Trensorflowでは処理を「データフロー」で行うのでセッションの中を
実行しないといけない。そのためにrun関数を使う
最近kotlin始めました。。。javafxの@FXMLのid定義
@FXML var button : Button? = null
上のコードはkotlinで変数定義は無理やりnulぶち込むと思い込んだコードですね。
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
@FXML lateinit var button : Button
↑lateinitはnull-warpの回数を減らせる!!
notnullであることが保証される時はlateinitで定義!!!
AOJ- 0098 Maximum Sum Sequence II Scala
Maximum Sum Sequence II Scala
部分行列の和の最大値 | Aizu Online Judge
何回も見たことのあるような問題。。。。。
まぁとりあえず、この問題の最初の考えは、
左上から右下までの総和を出す(各場所の)。
Sample Input
3 1 -2 3 -4 5 6 7 8 -9
で考える
1 -2 3 → 1 0 0 → 1 -1 0 → 1 -1 2 → 1 -1 2 → 1 -1 2 → 1 -1 2 -4 5 6 → 0 0 0 → 0 0 0 → 0 0 0 → -3 0 0 → -3 0 0 → -3 0 9 7 8 -9 → 0 0 0 → 0 0 0 → 0 0 0 → 0 0 0 → 0 0 0 → 0 0 0 ↓↓↓ ↓↓↓←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←← 1 -1 2 → 1 -1 2 → 1 -1 2 -3 0 9 → -3 0 9 → -3 0 9 4 0 0 → 4 15 0 → 4 15 15
計算 ... map [ i - 1 ] [ j ] + [ i ] [ j - 1 ] - [ i - 1 ] [ j - 1 ]
次に
総和からその場から区切りの場所まで削る。
削る際に重なってる部分多く削るので、その部分を足す
計算... [最初での総和] - [区切りまでの削り] - [区切りまでの削り] + [削りの重なり]
ではソースコード!!!!
import scala.io.StdIn._ import scala.math._ object Main{ def main(args: Array[String]): Unit = { lazy val n = readInt var map = Array.ofDim[Int](n + 1, n + 1) for(i <- 1 to n){ var temp = readLine.split(" ").map(_.toInt) for(j <- 1 to n)map(i)(j) = map(i)(j - 1) + map(i - 1)(j) - map(i - 1)(j - 1) + temp(j - 1) } var max = map(1)(1) for(i <- 1 to n ;j <- 1 to n;k <- i to n;l <- j to n) max = math.max(max,map(k)(l) + map(i - 1)(j - 1) - map(i - 1)(l) - map(k)(j - 1)) println(max) } }
Scala #2 String(複数行の処理)
今日は文字列について確認していこうと思う。。
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
複数行の処理
(String).lines.foreach の使用により複数行の処理が可能!!
object String { def main(args: Array[String]): Unit = { "1\n2\n2\n3".lines.foreach { x => print(x + " ") } //出力結果(1 2 2 3 ) } }
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
少しずつscalaを振り返ろう
Scala #1 Lazyについて - YMChihiroの日記
Scala #2 String(複数行の処理) - YMChihiroの日記
Scala #1 Lazyについて
少しずつscalaを振り返ろう
Scala #1 Lazyについて - YMChihiroの日記
lazyとは
valの変数やフィールドを宣言することで、
変数の参照されるまでの評価を遅らせれます
import scala.io.StdIn._ object Main { def main(args: Array[String]): Unit = { var variable = readInt lazy val temp = variable * readInt // ここでは計算されない println(temp)//ここで計算が実行される。 } }続きを読む
Volume 100 of AOJ , ID 10000 ~ 10024
pythonの文法を忘れてしまったんで、復習がてら aoj の Volume100をやった二日間それで #0 ~ #24までの記録を示す!
Hello World | プログラミング入門 | Aizu Online Judge
print "Hello World"
x の3乗 | プログラミング入門 | Aizu Online Judge
print pow(input(),3);
長方形 | プログラミング入門 | Aizu Online Judge
x, y = map(int, raw_input().split()) print x * y, x * 2 + y * 2
大小 等価 | プログラミング入門 | Aizu Online Judge
a, b = map(int, raw_input().split(" ")) if a == b: print "a == b" elif a > b: print "a > b" elif a < b: print "a < b"
3つの数の整列 | プログラミング入門 | Aizu Online Judge
import java.util.Arrays; import java.util.Scanner; class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a []= new int[3]; for(int i = 0; i < 3; i++){ a[i] = sc.nextInt(); } Arrays.sort(a); System.out.println(a[0] + " " + a[1] + " " + a[2]); } }
2つの数の交換 | プログラミング入門 | Aizu Online Judge
import java.util.Arrays; import java.util.Scanner; class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a []= new int[3]; for(int i = 0 ; i < 1000; i++){ System.out.println("Hello World"); } } }
Print Test Cases | Aizu Online Judge
i = 0 while 1: x = raw_input() i += 1 if x == '0': break print 'Case %s: %s' % (i,x)
2つの数の交換 | プログラミング入門 | Aizu Online Judge
import java.util.Arrays; import java.util.Scanner; class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a []= new int[2]; while(sc.hasNext()){ a[0] = sc.nextInt(); a[1] = sc.nextInt(); if((a[0] | a[1]) == 0)break; Arrays.sort(a); System.out.println(a[0] + " " + a[1]); } } }
A / B 問題 | プログラミング入門 | Aizu Online Judge
import java.text.DecimalFormat; import java.util.Arrays; import java.util.Scanner; class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); double a []= {sc.nextDouble(),sc.nextDouble()}; DecimalFormat f = new DecimalFormat("0.00000"); System.out.println((int)(a[0]/a[1]) + " " + (int)(a[0]%a[1]) + " " + f.format(a[0]/a[1])); } }
円 | プログラミング入門 | Aizu Online Judge
import math a = float(raw_input()) print"%.6f %.6f" %(math.pi*a**2,2*math.pi*a)
単純計算器 | プログラミング入門 | Aizu Online Judge
#include <iostream> using namespace std; int main(void) { int a, b; char op; while (cin >> a >> op >>b) { if (op == '?')break; int r; switch (op) { case '+': r = a + b; break; case '-': r = a - b; break; case '*': r = a * b; break; case '/': r = a / b; break; } cout << r << std::endl; } return 0; }
数列の反転 | プログラミング入門 | Aizu Online Judge
n = int(raw_input()) a = map(int, raw_input().split()) a.reverse() print ' '.join(map(str, a))
長方形の描画 | プログラミング入門 | Aizu Online Judge
while True: h,w = map(int, raw_input().split()) if w == 0 : break for i in range(h): print "#" * w print
フレームの描画 | プログラミング入門 | Aizu Online Judge
while True: h,w = map(int, raw_input().split()) if w == 0 : break print "#" * w for i in range(h - 2): print "#" + "." *(w - 2)+ "#" print "#" * w print
チェック柄の描画 | プログラミング入門 | Aizu Online Judge
while True: H, W = map(int,raw_input().split()) if W == 0: break for i in xrange(H): if W % 2 == 0: if i % 2 == 0: print "#" + ".#" * int(W / 2 - 1) + "." else: print "." + "#." * int(W / 2 - 1) + "#" else: if i % 2 == 0: print "#" + ".#" * int(W / 2) else: print "." + "#." * int(W / 2) print
なくなったカードの発見 | プログラミング入門 | Aizu Online Judge
card = [] N = int(raw_input()) for n in range(N): card.append(raw_input()) mojul = ["S","H","C","D"] for i in mojul: for j in range(13): if not i + " " + str(j + 1) in card: print i,(j + 1)
成績の判定 | プログラミング入門 | Aizu Online Judge
while True: m,f,r = map(int, raw_input().split()) if m == -1 and f == -1 and r == -1:break sum = m + f if m == -1 or f == -1 or sum < 30:print 'F' elif sum >= 80:print 'A' elif 80 > sum >= 65:print 'B' elif 65> sum >= 50:print 'C' elif 50 > sum >= 30: if r >= 50: print 'C' else: print 'D'
組み合わせの数 | プログラミング入門 | Aizu Online Judge
while True: n,x = map(int,raw_input().split()) count = 0; if n == 0 and x == 0:break for i in range(1,n + 1): for j in range(1,n + 1): if i == j:break for k in range(1,n + 1): if j == k:break if i + j + k == x: count+=1 break print count
数字の和 | プログラミング入門 | Aizu Online Judge
while True: n = str(raw_input()) if n == "0":break sum = 0 for i in n: sum += int(i) print sum
文字のカウント | プログラミング入門 | Aizu Online Judge
line = "" while True: try: line += raw_input() except:break for i in range(26): c = chr(i + 97) print '%s : %d' % (c, line.lower().count(c))
最初の単語 | プログラミング入門 | Aizu Online Judge
import sys; n = input() print sorted(sys.stdin.readlines())[0] ,
単語の検索 | プログラミング入門 | Aizu Online Judge
str = raw_input() lines = [] while True: Sentence = raw_input() if Sentence == 'END_OF_TEXT':break lines += Sentence.lower().split() print lines.count(str)
シャッフル | プログラミング入門 | Aizu Online Judge
while 1: line = raw_input() if line == "-":break n = int(raw_input()) for i in range(n): integer = int(raw_input()) line = line[integer:len(line) + 1] + line[0:integer] print line
距離 | プログラミング入門 | Aizu Online Judge
import java.awt.geom.Point2D; import java.text.DecimalFormat; import java.util.Scanner; class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); Point2D.Double point = new Point2D.Double(); System.out.println(new DecimalFormat("0.00000000").format(point.distance(sc.nextDouble(),sc.nextDouble(),sc.nextDouble(),sc.nextDouble()))); } }