{"action":"created","issue":{"url":"https://api.github.com/repos/gcp/leela-zero/issues/2107","repository_url":"https://api.github.com/repos/gcp/leela-zero","labels_url":"https://api.github.com/repos/gcp/leela-zero/issues/2107/labels{/name}","comments_url":"https://api.github.com/repos/gcp/leela-zero/issues/2107/comments","events_url":"https://api.github.com/repos/gcp/leela-zero/issues/2107/events","html_url":"https://github.com/gcp/leela-zero/issues/2107","id":394849689,"node_id":"MDU6SXNzdWUzOTQ4NDk2ODk=","number":2107,"title":"idea of slightly reduce weight size","user":{"login":"l1t1","id":23301704,"node_id":"MDQ6VXNlcjIzMzAxNzA0","avatar_url":"https://avatars1.githubusercontent.com/u/23301704?v=4","gravatar_id":"","url":"https://api.github.com/users/l1t1","html_url":"https://github.com/l1t1","followers_url":"https://api.github.com/users/l1t1/followers","following_url":"https://api.github.com/users/l1t1/following{/other_user}","gists_url":"https://api.github.com/users/l1t1/gists{/gist_id}","starred_url":"https://api.github.com/users/l1t1/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/l1t1/subscriptions","organizations_url":"https://api.github.com/users/l1t1/orgs","repos_url":"https://api.github.com/users/l1t1/repos","events_url":"https://api.github.com/users/l1t1/events{/privacy}","received_events_url":"https://api.github.com/users/l1t1/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":21,"created_at":"2018-12-30T06:15:24Z","updated_at":"2019-01-04T01:49:57Z","closed_at":null,"author_association":"NONE","body":"if the difference of( round to 2 digit - round to 3 digit) is small enough, we can use round to 2 digit result.\r\nI use the the ratio 0.01 and the result to many different size weight is following\r\n```\r\nD:\\>c:\\python37\\python quantize_weights23.py 57.txt\r\nWeight file difference L2-norm: 0.3201855506392345\r\n\r\nD:\\>c:\\python37\\python quantize_weights23.py 91.txt\r\nWeight file difference L2-norm: 0.1640525086752053\r\n\r\nD:\\>c:\\python37\\python quantize_weights23.py 116.txt\r\nWeight file difference L2-norm: 4.988491974049602\r\n\r\nD:\\>c:\\python37\\python quantize_weights23.py elf_converted_weights.txt\r\nWeight file difference L2-norm: 0.00405287248899887\r\n\r\nthe orginal code result\r\n\r\nD:\\>c:\\python37\\python quantize_weights.py 57.txt\r\nWeight file difference L2-norm: 0.31965124587016847\r\n\r\nD:\\>c:\\python37\\python quantize_weights.py 91.txt\r\nWeight file difference L2-norm: 0.040919122536652267\r\n\r\nD:\\>c:\\python37\\python quantize_weights.py 116.txt\r\nWeight file difference L2-norm: 0.04882264516397222\r\n\r\nD:\\>c:\\python37\\python quantize_weights.py elf_converted_weights.txt\r\nWeight file difference L2-norm: 0.0035875174236782307 \r\n\r\n```\r\nthe modifed code\r\n```\r\n#!/usr/bin/env python3\r\nimport sys, os, argparse\r\n\r\ndef format_n(x):\r\n    x = float(x)\r\n    x2 =float('{:.2g}'.format(x))\r\n    if x!=0 and abs(x-x2)/abs(x) <0.01:\r\n     x = '{:.2g}'.format(x)\r\n    else: \r\n     x = '{:.3g}'.format(x)\r\n    x = x.replace('e-0', 'e-')\r\n    if x.startswith('0.'):\r\n        x = x[1:]\r\n    if x.startswith('-0.'):\r\n        x = '-' + x[2:]\r\n    return x\r\n\r\nif __name__ == \"__main__\":\r\n\r\n    parser = argparse.ArgumentParser(\r\n            description='Quantize network file to decrease the file size.')\r\n    parser.add_argument(\"input\",\r\n        help='Input file', type=str)\r\n\r\n    parser.add_argument(\"-o\", \"--output\",\r\n        help='Output file. Defaults to input + \"_quantized\"',\r\n        required=False, type=str, default=None)\r\n\r\n    args = parser.parse_args()\r\n\r\n    if args.output == None:\r\n        output_name = os.path.splitext(sys.argv[1])\r\n        output_name = output_name[0] + '_quantized23' + output_name[1]\r\n    else:\r\n        output_name = args.output\r\n    output = open(output_name, 'w')\r\n\r\n    calculate_error = True\r\n    error = 0\r\n\r\n    with open(args.input, 'r') as f:\r\n        for line in f:\r\n            line = line.split(' ')\r\n            lineq = list(map(format_n, line))\r\n\r\n            if calculate_error:\r\n                e = sum((float(line[i]) - float(lineq[i]))**2 for i in range(len(line)))\r\n                error += e/len(line)\r\n            output.write(' '.join(lineq) + '\\n')\r\n\r\n    if calculate_error:\r\n        print('Weight file difference L2-norm: {}'.format(error**0.5))\r\n\r\n    output.close()\r\n\r\n```"},"comment":{"url":"https://api.github.com/repos/gcp/leela-zero/issues/comments/451333714","html_url":"https://github.com/gcp/leela-zero/issues/2107#issuecomment-451333714","issue_url":"https://api.github.com/repos/gcp/leela-zero/issues/2107","id":451333714,"node_id":"MDEyOklzc3VlQ29tbWVudDQ1MTMzMzcxNA==","user":{"login":"l1t1","id":23301704,"node_id":"MDQ6VXNlcjIzMzAxNzA0","avatar_url":"https://avatars1.githubusercontent.com/u/23301704?v=4","gravatar_id":"","url":"https://api.github.com/users/l1t1","html_url":"https://github.com/l1t1","followers_url":"https://api.github.com/users/l1t1/followers","following_url":"https://api.github.com/users/l1t1/following{/other_user}","gists_url":"https://api.github.com/users/l1t1/gists{/gist_id}","starred_url":"https://api.github.com/users/l1t1/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/l1t1/subscriptions","organizations_url":"https://api.github.com/users/l1t1/orgs","repos_url":"https://api.github.com/users/l1t1/repos","events_url":"https://api.github.com/users/l1t1/events{/privacy}","received_events_url":"https://api.github.com/users/l1t1/received_events","type":"User","site_admin":false},"created_at":"2019-01-04T01:49:57Z","updated_at":"2019-01-04T01:49:57Z","author_association":"NONE","body":"@wonderingabout\r\nwhen I edit https://github.com/l1t1/lz19/edit/fixcode-next/training/tf/quantize_weights.py\r\nthe 'commit  changes ' button was disable, another way to commit?"}}
{"actor":{"display_login":"l1t1"}}