def cluster_spec(num_workers: int, num_ps: int, num_masters: int=0) ->dict:
    """
    Generate a cluster specification (for distributed Tensorflow).
    """
    cluster = {}
    port = 12222
    all_ps = []
    host = '127.0.0.1'
    for _ in range(num_ps):
        all_ps.append('{}:{}'.format(host, port))
        port += 1
    cluster['ps'] = all_ps
    all_workers = []
    for _ in range(num_workers):
        all_workers.append('{}:{}'.format(host, port))
        port += 1
    cluster['worker'] = all_workers
    if num_masters > 0:
        all_masters = []
        for _ in range(num_masters):
            all_masters.append('{}:{}'.format(host, port))
            port += 1
        cluster['master'] = all_masters
    return cluster
